以编程方式更改边距不能按预期工作

安德烈

我有一个GUI,其中最初的边距innerLayout0,10,0,100,我打算hideinfoarea()删除infoLayout并更改其中的边距,0, 10,0,10showInfoarea()应恢复初始的边距并再次显示infoLayout。

不幸的是,当我设置这些边距时,某些Layout不能很好地显示,例如标题太接近mainLayout,并且当我重新启用它时,infoLayout会覆盖一张图像。尽管在我与布局编辑器的测试中,页边距还是完美的。

此外showInfoarea(),如果我简单地反转的操作,为什么为什么不能将布局边距恢复到原始状态hideInfoarea()

这是我的代码

 RelativeLayout mainLayout;
    LinearLayout innerLayout;

    public void hideInfoarea(){
        mainLayout = (RelativeLayout) findViewById(R.id.mainContainer);
        innerLayout = (LinearLayout) findViewById(R.id.innerLayout);
        mainContainer.removeView(infoLayout);

        //adjust margins
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) innerLayout.getLayoutParams();  
        params.setMargins(0, 10, 0, 10);
        innerLayout.setLayoutParams(params);
    }

public void showInfoarea(){
        mainLayout = (RelativeLayout) findViewById(R.id.mainContainer);
        innerLayout = (LinearLayout) findViewById(R.id.innerLayout);
        mainContainer.addView(infoLayout);

        //adjust margins
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) innerLayout.getLayoutParams();  
        params.setMargins(0, 10, 0, 100);
        innerLayout.setLayoutParams(params);
    }

这是XML

   <!-- The main content view -->
    <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"
            android:padding="5dp"
            android:gravity="fill_horizontal|fill_vertical"
            android:background="@color/Platinum"
            android:id="@+id/mainLayout">

        <TextView
                android:id="@+id/txtTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="Sample"
                android:textSize="12dp"
                android:layout_marginBottom="10dp"/>

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:id="@+id/innerlayout"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="100dp">

            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:id="@+id/imageView"
                    android:scaleType="fitXY"
                    android:src="@drawable/rectangle"
                    android:layout_marginBottom="100dp"
                    android:layout_marginTop="10dp"/>
        </LinearLayout>

        <LinearLayout
                android:layout_width="400dp"
                android:layout_height="100dp"
                android:layout_alignParentBottom="true"
                android:background="@drawable/background_label_mm"
                android:layout_centerHorizontal="true"
                android:orientation="vertical"
                android:id="@+id/infoLayout"
                android:layout_margin="5dp">

        </LinearLayout>

    </RelativeLayout>


    <!-- The navigation drawer -->

    <ListView
            android:id="@+id/drawer"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#F3F3F4"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"/>

</android.support.v4.widget.DrawerLayout>
弗洛

请小心,因为您可以阅读ReferencesetMargins(int left, int top, int right, int bottom)方法使用px和您的布局使用dp您必须进行如下转换:

// where "dp" is your value dip
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
                 (float) dp, getResources().getDisplayMetrics());  
// then set your margins
params.setMargins(0, px, 0, px);  

看到这个答案:从Java代码在DIP中指定尺寸的正确方法是什么?相反的:将像素转换为dp

另外,您还可以阅读《参考》中的另一篇文章:

设置边距,以像素为单位。需要完成对requestLayout()的调用,以便考虑新的边距。

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

QPrinter页边距不能按预期方式工作

来自分类Dev

:not()不能按预期工作

来自分类Dev

:not()不能按预期工作

来自分类Dev

Pyqt:QTextCursor.WordUnderCursor不能按预期方式工作

来自分类Dev

缀符号和with(...)不能按我预期的方式工作

来自分类Dev

SwiftUI:listRowInsets不能按预期方式工作

来自分类Dev

条件绑定不能按预期方式工作

来自分类Dev

Clickhouse:runningAccumulate()不能按我预期的方式工作

来自分类Dev

垂直居中与变换不能按预期方式工作

来自分类Dev

访问授权的数组值不能按预期方式工作

来自分类Dev

字符不能按预期方式在c中工作

来自分类Dev

AutoResizeTextView不能按预期工作

来自分类Dev

getElementById不能按预期工作?

来自分类Dev

递归IEnumerable不能按预期工作?

来自分类Dev

jQuery slideDown不能按预期工作

来自分类Dev

递归函数不能按预期工作

来自分类Dev

Javascript RegExp'*'不能按预期工作

来自分类Dev

strtoul()不能按预期工作?

来自分类Dev

sendRedirect()不能按预期工作

来自分类Dev

$ nin与$ elemMatch不能按预期工作

来自分类Dev

终于不能按预期工作

来自分类Dev

whenAll()不能按预期工作

来自分类Dev

arraylist不能按预期工作

来自分类Dev

QGridLayout不能按预期工作

来自分类Dev

unset()不能按预期工作

来自分类Dev

onclick不能按预期工作

来自分类Dev

bcrypt nodejs不能按预期工作

来自分类Dev

NavigationManager不能按预期工作?

来自分类Dev

bcrypt nodejs不能按预期工作