【问题标题】:Unable to set LinearLayout with margins programmatically无法以编程方式设置带边距的 LinearLayout
【发布时间】:2023-03-15 09:42:01
【问题描述】:

我想要有两个 LinearLayouts,每个都用一些边距分隔。下面的布局完美。

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="1">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="5dp"
        android:background="@drawable/border_style">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Uday"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="5dp"
        android:background="@drawable/border_style">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="ABCD"/>
    </LinearLayout>
</GridLayout>

现在我想以编程方式进行,因为 textview 值是动态的,并且 我尝试使用 LayoutParameters 的 setMargins,但没有奏效。我在循环中尝试了下面的代码,但我看不到用边距分隔的布局。

LinearLayout lLayour = new LinearLayout(getActivity());
lLayour.setOrientation(LinearLayout.VERTICAL);
lLayour.setBackgroundResource(R.drawable.border_style);
LinearLayout.LayoutParams llLP=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llLP.setMargins(5,5,5,5);
lLayour.setLayoutParams(llLP);

TextView tv=new TextView(getActivity());
tv.setText("1");
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

lLayour.addView(tv);

gridLayout.addView(lLayour);

后来知道了MarginLayoutParams,但还是没用:

LinearLayout lLayour = new LinearLayout(getActivity());
lLayour.setOrientation(LinearLayout.VERTICAL);
lLayour.setBackgroundResource(R.drawable.border_style);
LinearLayout.LayoutParams llLP=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lLayour.setLayoutParams(llLP);
setMargins(lLayour, 50, 50, 50, 50);

TextView tv=new TextView(getActivity());
tv.setText("1");
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

lLayour.addView(tv);
gridLayout.addView(lLayour);

private void setMargins (View view, int left, int top, int right, int bottom) {
    if (view.getLayoutParams() instanceof LinearLayout.MarginLayoutParams) {
        LinearLayout.MarginLayoutParams p = (LinearLayout.MarginLayoutParams) view.getLayoutParams();
        p.setMargins(left, top, right, bottom);
        view.requestLayout();
    }
}

我的border_style.xml文件内容是:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <stroke android:width="2dp" android:color="#44aa77"/>
    <solid android:color="#ffffff" />
    <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp"/>
</shape>

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    根据上面提到的细节,如果LinearLayout 不占利润,您也可以尝试添加padding 而不是margins

    以下是以编程方式将填充设置为LinearLayout 的代码:

    mLinearLayout.setPadding(20,20,20,20);
    

    【讨论】:

      猜你喜欢
      • 2011-01-29
      • 1970-01-01
      • 2011-06-22
      • 2014-10-25
      • 1970-01-01
      • 2014-11-26
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多