【问题标题】:Change RelativeLayout Height更改相对布局高度
【发布时间】:2014-05-07 22:35:18
【问题描述】:

我想改变相对布局的高度。当您按下按钮时,我想将其从 70dp 更改为 80dp。我检查了按钮是否适用于可见性变化并且确实有效。

我已经搜索过答案,请参阅链接Changing relative layout programmatically

在那个答案中提到了这段代码:

 RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
 rl.getLayoutParams().height = 100;
 rl.getLayoutParams().width = 100;

我尝试了上面的代码,但它不起作用,没有任何反应。查看我的代码:

  public void next (View view){
    if (view == mnext){

                RelativeLayout.LayoutParams paramsT1 = (RelativeLayout.LayoutParams)mT1layout.getLayoutParams();
                // Changes the height and width to the specified *pixels*
                paramsT1.height = 300;                

      }
   }

这是我的 .XML,Relativelayout 被放置在一个线性布局中。

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="70dp"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="70dp"
        android:background="#314ebd"
        android:id="@+id/LayoutT1score"
        android:layout_toLeftOf="@+id/LayoutT2score">

        <TextView
            android:id="@+id/player1"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:text="0"
            android:textSize="24dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:textColor="#ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Team 1"
            android:id="@+id/lblTeam1"
            android:layout_above="@+id/player1"
            android:layout_alignLeft="@+id/player1"
            android:layout_alignStart="@+id/player1"
            android:textSize="24dp"
            android:textColor="#ffffff" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="60dp"
        android:id="@+id/LayoutT2score"
        android:background="#fbff62"
        >

        <TextView
            android:id="@+id/player2"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number"
            android:text="0"
            android:textSize="24dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="0dp"
            android:textColor="#000000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Team 2"
            android:id="@+id/lblTeam2"
            android:textSize="24dp"
            android:textColor="#000000"
            android:layout_above="@+id/player2"
            android:layout_alignLeft="@+id/player2"
            android:layout_alignStart="@+id/player2" />


    </RelativeLayout>
  </LinearLayout>

【问题讨论】:

    标签: android height android-relativelayout


    【解决方案1】:

    更改高度后忘记调用mT1layout.setLayoutParams(paramsT1)mT1layout.invalidate()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2015-03-31
      • 1970-01-01
      相关资源
      最近更新 更多