[英]dynamically set LinearLayout height/width in relativelayout
我在相對布局中有一個線性布局。 我需要能夠根據屏幕尺寸動態設置線性布局的高度。 我遇到了一些困難。 我怎樣才能做到這一點?
主要的:
public class MainActivity extends Activity {
LinearLayout L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
L=(LinearLayout)findViewById(R.id.top);
L.setLayoutParams(new LinearLayout.LayoutParams(1080, 400));
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:id="@+id/top"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
設置布局參數時,它必須是父級的。
開關:
L.setLayoutParams(new LinearLayout.LayoutParams(1080, 400));
至:
L.setLayoutParams(new RelativeLayout.LayoutParams(1080, 400));
在您的LinearLayout中,只需進行設置
android:layout_height="0dp"
android:layout_weight="1"
賈斯汀回答
L.setLayoutParams(new RelativeLayout.LayoutParams(1080, 400));
如果您已經設置了布局,則可能還需要
L.requestLayout();
在 Kotlin 中,您可以執行以下操作:
L.layoutParams = RelativeLayout.LayoutParams(1080, 400)
然后調用L.requestLayout()
應用更改
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.