android - 如何以编程方式在 Android 中包含布局?

标签 android android-layout parameters include xamarin.android

我正在寻找一种以编程方式包含布局的方法,而不是像我的示例中那样使用 XML 标记 include:

  <include layout="@layout/message"  
           android:layout_width="match_parent" 
           android:layout_height="match_parent" 
           android:layout_weight="0.75"/>

请以编程方式更改此参数“layout="@layout/message”。

知道怎么做吗?

最佳答案

使用 ViewStub 代替 include:

<ViewStub
    android:id="@+id/layout_stub"
    android:inflatedId="@+id/message_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.75" />

然后在代码中,获取对 stub 的引用,设置其布局资源,并对其进行扩充:

ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
stub.setLayoutResource(R.layout.whatever_layout_you_want);
View inflated = stub.inflate();

关于android - 如何以编程方式在 Android 中包含布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18999601/

相关文章:

java - CardView 列表不在我的应用程序中滚动

c++ - 如何将矩阵参数发送到 C++ 中的 main 函数

javafx - 传递参数JavaFX FXML

Python - 自动化 MySQL 查询 : passing parameter

java - 连接拒绝异常

Android Activity 转换顺序

android - 如何设置 TimePicker 显示格式为 24 小时

java - TypeFace.createFromAsset 找不到字体错误

android - android中的布局设计

android - 重定向到设置菜单后停止 Intent