如何以编程方式更改RelativeLayout中linearLayout的顶部位置

时间:2015-03-12 09:45:53

标签: android android-layout android-linearlayout

我想改变ReletiveLayout内部的linearlayout。 但是当我使用settop时它不起作用! 而且我找不到好的教程

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:padding="5dp" >

<LinearLayout
    android:id="@+id/remotebox"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <TextView
        android:id="@+id/remote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="WWWWWWWWWWW"
        android:textSize="30dp" 
        android:layout_gravity="left"
        />

    <TextView
        android:id="@+id/ver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TTTTTT" 
        android:layout_gravity="right"
        />
</LinearLayout>

我希望更改“@ + id / remotebox”布局位置和宽度以及高度。 关于这个问题的任何建议或好教程的链接? 抱歉英语不好

2 个答案:

答案 0 :(得分:2)

试试这个

   LinearLayout linearLayout= (LinearLayout)findViewById(R.id.remotebox);
    linearLayout.getLayoutParams().width=50;//dimensions
    linearLayout.requestLayout(); 
    linearLayout.setTranslationY(800);//position

希望这会对你有所帮助。

答案 1 :(得分:0)

就我而言,以下代码有效:

    // step1.  get the target view
    LinearLayout thisView = (LinearLayout)rootView.findViewById(R.id.tab3_bind_device_page);
    // step2. get the LayoutPrams instance. 
    // notice: in this case, the LinearLayout is wrapped by this RelativeLayout, 
    // so here we have to use the RelativeLayout.LayourParams
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

    // change the margins 
    // in fact you should convert -25f from "px" to "dp", if possible.
    lp.setMargins(0, -25f, 0, 0);

    thisView.setLayoutParams(lp);