无法以编程方式设置带边距的LinearLayout

时间:2018-04-03 06:56:15

标签: android android-layout android-linearlayout

我希望有两个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>

1 个答案:

答案 0 :(得分:0)

根据提到的详细信息,如果LinearLayout未占用边距,您也可以尝试添加padding而不是margins

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

mLinearLayout.setPadding(20,20,20,20);
相关问题