将LinearLayout动态定位在RelativeLayout中

时间:2015-03-17 16:28:25

标签: android android-layout

我试图将LinearLayout置于RelativeLayout内的另一个元素下方。

这是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_android"
    android:weightSum="1"
    android:id="@+id/family_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:id="@+id/relativeLayout">

        <ImageView
            android:layout_width="180dp"
            android:layout_height="62dp"
            android:id="@+id/imageView2"
            android:src="@drawable/test_logo"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <ImageButton
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/menu_btn"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:background="@drawable/menu_icon" />

    </RelativeLayout>


</RelativeLayout>

我正在动态创建一个新的LinearLayout,这样可以正常工作。问题是我似乎无法将新创建的元素放在RelativeLayout内的现有元素下面。

基本上,我创建了一个新的LinearLayout,我希望将其放在ID为RelativeLayout的{​​{1}}下方。这是我到目前为止的代码。

relativeLayout

更新

// GET ACTIVITY PARENT
RelativeLayout parent = (RelativeLayout) findViewById(R.id.family_parent);

RelativeLayout.LayoutParams parentParams = (RelativeLayout.LayoutParams) parent.getLayoutParams();

parentParams.addRule(RelativeLayout.BELOW, R.id.relativeLayout);

// CREATE CHILD LAYOUTS
LinearLayout newLLayout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams LLayoutParams = new LinearLayout.LayoutParams(
        100, ViewGroup.LayoutParams.MATCH_PARENT);

newLLayout.setLayoutParams(parentParams);

newLLayout.setBackgroundColor(Color.parseColor("#FF0000"));

parent.addView(newLLayout, LLayoutParams);

发出此错误:

// GET ACTIVITY PARENT
RelativeLayout parent = (RelativeLayout) findViewById(R.id.family_parent);
RelativeLayout.LayoutParams parentParams = (RelativeLayout.LayoutParams) parent.getLayoutParams();


LinearLayout newLLayout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams LLayoutParams = new LinearLayout.LayoutParams(
        100, ViewGroup.LayoutParams.MATCH_PARENT);

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
parentParams.addRule(RelativeLayout.BELOW, R.id.relativeLayout);

newLLayout.setLayoutParams(layoutParams);

parent.addView(newLLayout, LLayoutParams);

2 个答案:

答案 0 :(得分:0)

id设置为LinearLayout

在添加规则时使用id

params.addRule(RelativeLayout.BELOW, linearLayout.getId());

答案 1 :(得分:0)

我认为你对LayoutParams感到困惑。将LayoutParams设置为视图时,必须使用父ViewGroups LayoutParams。

LinearLayout newLLayout = new LinearLayout(getApplicationContext());

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
parentParams.addRule(RelativeLayout.BELOW, R.id.relativeLayout);

newLLayout.setLayoutParams(layoutParams);

应该这样做。