【问题标题】:adding a layout in another layout [duplicate]在另一个布局中添加布局[重复]
【发布时间】:2011-06-15 01:51:40
【问题描述】:

如何在另一个布局中添加一个布局。我在 tblrow.XML 中创建了这个布局。所以我想在 menu.XML 中添加这些行。我想根据行数添加这些行。我该怎么做那。如果我添加它,我如何识别每一行。请帮助我。我需要 Java 中的解决方案而不是 XML 中的解决方案。 我的代码是

<TableRow android:id="@+id/tblRowMovies" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/btn_backgrnd"  android:clickable="true" android:focusable = "true" android:layout_weight="1.0">
<ImageView android:layout_width="wrap_content" android:layout_height="fill_parent" android:src="@drawable/movie" android:layout_gravity="center_vertical|center_horizontal"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="MOVIES" android:layout_gravity="center_vertical" android:paddingLeft="20dp" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold">
</TextView>        

.

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您可以在另一个布局文件中包含一个布局文件,这种技术称为使用包含标签重用 Android 布局。 例如:

    <include android:id="@+id/myid1" layout="@layout/workspace_screen" />
    

    a blogpost by Android developers. 对此进行了很好的解释。

    又一篇关于Android developer site explains the layout re-usability的文章。

    【讨论】:

    【解决方案2】:

    创建文件titlebar.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width=”match_parent”
        android:layout_height="wrap_content"
        android:background="@color/titlebar_bg">
    
        <ImageView android:layout_width="wrap_content"
                   android:layout_height="wrap_content" 
                   android:src="@drawable/gafricalogo" />
    </FrameLayout>
    

    在主布局中:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      相关资源
      最近更新 更多