【问题标题】:Putting children inside include tag将孩子放入包含标签
【发布时间】:2014-11-10 06:42:22
【问题描述】:

所以根据这个-http://developer.android.com/training/improving-layouts/reusing-layouts.html,我可以使用include标签来重用布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="@color/app_bg"
android:gravity="center_horizontal">

<include layout="@layout/titlebar"/>

<TextView android:layout_width=”match_parent”
          android:layout_height="wrap_content"
          android:text="@string/hello"
          android:padding="10dp" />

...

</LinearLayout>

是否可以将元素放在包含标签中。

例如:

<include layout="@layout/titlebar">
    <Button/>
</include>

如果没有,我怎样才能得到类似的效果。

我的目标是能够使包含标签成为根元素。

【问题讨论】:

    标签: android android-xml


    【解决方案1】:

    要从 xml 中放入元素,您必须将它们添加到 titlebar.xml not&lt;include&gt; &lt;/include&gt;。 否则你可以从代码中添加你的元素:

    ViewGroup layout = (ViewGroup) findViewById(R.id.your_title_bar_id);
    Button btn = new Button(this);
    btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    layout.addView(btn );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 2020-03-13
      • 2020-02-25
      • 1970-01-01
      • 2017-01-09
      相关资源
      最近更新 更多