【发布时间】:2015-08-19 01:27:18
【问题描述】:
我正在尝试通过创建一个 XML 模板来动态生成用户界面,该模板最终将附加到在 Activity XML 中静态定义的 LinearLayout 父级。静态定义的 LinearLayout 父级封装在 ScrollView 中。
在我的示例代码中,我似乎只能遍历模板的父 LinearLayout 而不能遍历其子模板。我也没有得到它的任何属性、按钮或图像。我正在使用 LayoutInflater 获取 XMl 并将其作为 LinearLayout 返回,因为它是我的模板文件的父级。任何想法或建议将不胜感激。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
Context context = getApplicationContext();
final LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout b = (LinearLayout)inflater.inflate(R.layout.event, null);
LinearLayout lLayout = (LinearLayout)findViewById(R.id.main_feed_parent);
lLayout.addView(b);
}
这是我试图动态添加到父 LinearLayout 的复杂 xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="325dp"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="17"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="15"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="20"
android:orientation="vertical"
android:weightSum="100">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="80"
android:orientation="horizontal"
android:weightSum="100">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_weight="50"
android:paddingBottom="20dp"
android:src="@drawable/thumbs1" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_weight="50"
android:paddingTop="5dp"
android:text="343"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="12"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_weight="33"
android:orientation="horizontal"
android:text="Join"
android:textSize="11dp" />
</LinearLayout>
</LinearLayout>
【问题讨论】:
-
我导入了您的代码,我可以很好地显示所有内容。你能分享
R.layout.activity_feed的xml吗? -
N.T.感谢您的帮助,我已经用更多信息更新了我的问题。我没有看到任何加载的图像或按钮。
标签: java android xml android-layout templates