Android如何以编程方式将layout_below更改为TextView

时间:2015-05-26 13:54:56

标签: android

<TextView
    android:id="@+id/aaa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/xxx" />

如何以编程方式在TextView中删除或添加layout_below?

1 个答案:

答案 0 :(得分:1)

我看到两种方式。

第一种方式,您需要创建具有相同名称但位于文件夹layout-land中的新布局,请在此处复制所有内容并删除layout_below规则。 在这种情况下,您只需声明另一个不包含layout_below规则

的布局

见此截图:

2 layout for different orientation

这意味着,您为不同的方向定义了不同的布局。 在layout/my_layout.xml

<TextView
        android:id="@+id/aaa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/xxx" />
layout-land/my_layout.xml中的

<TextView
            android:id="@+id/aaa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

有关详细信息,请参阅this链接:

  

表2.配置限定符名称,名称为“Screen orientation”的行

第二种方式,在您的java代码中(例如onCreate()方法):

if(Activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) yourView.getLayoutParams();
    lp.addRule(RelativeLayout.BELOW, 0);
    yourView.setLayoutParams(lp);
}

<强> EDITED