簡體   English   中英

動態更改相對布局寬度和高度

[英]Change Relative layout width and height dynamically

嗨我在LinearLayout中使用以下布局結構

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv1"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:paddingLeft="10dp"
            android:textColor="#000" />

        <TextView
            android:id="@+id/tv2"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:gravity="right"
            android:paddingRight="10dp"
            android:textColor="#000" />
    </TableRow>
</TableLayout>

<ImageView
    android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="320px"
    android:layout_height="320px"
    android:gravity="center" >
</RelativeLayout>

並且想要從java文件中動態設置相對布局的寬度和高度,而不是在xml文件中將其設置為320px但不能這樣做,因為我將限制僅限於縱向模式,因此方向更改不是問題。 可以使用match_parent在全屏幕上設置相對布局,但我必須在屏幕上放置另一個視圖,這樣才有可能,或者我必須以另一種方式實現它...

一旦顯示,Android不會使用wrap_content刷新視圖布局。 即使使用invalidate()requestLayout() 因此,如果您添加子視圖或動態修改內容,則會被搞砸。

getLayoutParams().height = x plus requestLayout()是一個很好的解決方案,如果您知道“x”,並且如果您只需要執行它。 之后, LayoutParams中的wrap_content將丟失,因為您已覆蓋它。

為了解決這個問題,我編寫了一個靜態類,它重新計算大小並使用wrap_content強制更新視圖的布局。 使用的代碼和說明可在此處獲得:

https://github.com/ea167/android-layout-wrap-content-updater

請享用!

嘗試使用這個

getLayoutParams().height= x;
requestLayout(); or invalidate(); 

給.xml中的相對布局賦一個id:

android:id="@+id/relativelayout"

現在在你的java文件中寫這個:

RelativeLayout Rl = (RelativeLayout) findViewById(R.id.relativelayout);

RelativeLayout.LayoutParams layout_description = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedwidth,
                        RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedHeight);

Rl.setLayoutParams(layout_description);

從下面的代碼你可以得到設備的高度和寬度: -

Display display = getWindowManager().getDefaultDisplay();
    int width = (display.getWidth() );
    int height = (display.getHeight() );

paramsTop urrealative layout: -

現在你可以設置你想要的高度或寬度。

        paramsTop = new RelativeLayout.LayoutParams(width, height);

您可以使用LayoutParams動態設置視圖尺寸。 像這樣的東西:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;

我遇到了這個問題,這是動態改變相對布局寬度和高度的最佳解決方案

RelativeLayout relMain = (RelativeLayout) convertView.findViewById(R.id.relMain);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.width =200;
 params.height =200;
 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        relMain.setLayoutParams(params);

如果你打算改變高度,那么這將成為一個技巧。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM