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

How to change top position of linearLayout in ReletiveLayout progammaticlly

我想更改 ReletiveLayout 内部的线性布局。 但是当我使用 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”布局位置和宽度和高度。 关于这个问题或 link 好的教程有什么建议吗? 抱歉英语不好

试试这个

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

希望这对您有所帮助。

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

    // 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);