【问题标题】:How to add seperate layouts under each tab using TabLayout如何使用 TabLayout 在每个选项卡下添加单独的布局
【发布时间】:2012-05-30 08:35:14
【问题描述】:

我已按照本教程 (http://developer.android.com/resources/tutorials/views/hello-tabwidget.html) 完成了任务,一切正常。

但是现在我不确定如何在每个选项卡下添加不同的布局?应该将布局添加到 main.xml 并以某种方式从我为每个选项卡创建的 calasses 中调用布局?

如果有人能就此提出建议,我将不胜感激。

例如,我想在其中一个选项卡下添加表格布局,我该怎么做?

非常感谢

【问题讨论】:

    标签: android gwt-tablayoutpanel


    【解决方案1】:

    看这个例子很有用Example of the TabLayout

    修改了这个类Androidactivity.java

    public class AndroidActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.androidtab);
    

    将此xml 文件添加到Res/layout/androidtab.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"    
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"       
    android:gravity="top"       
    android:stretchColumns="1">
    <LinearLayout
        android:id="@+id/top_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/text"
            android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="20dp"
            android:text="Create Photo" />
    </LinearLayout>    
     <TextView            
            android:id="@+id/editUsername"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_weight="4"
            android:text="CREATE NEW RECORD"            
            android:layout_marginLeft="25dp"
            android:layout_marginTop="20dp"
            android:layout_marginRight="75dp"   
            android:textStyle="bold"
            android:padding="10dp"/>
    <TableLayout  
          android:id="@+id/tableLayout1"    
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"       
          android:gravity="top"       
          android:stretchColumns="1">       
      <TableRow
           android:id="@+id/row2"
           android:layout_marginTop="45dp"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
        <EditText
            android:inputType="text"
            android:id="@+id/editUsername"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_weight="2"
            android:singleLine="true"
            android:hint="input field1"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="2dip"
            android:layout_marginRight="75dp"   
            android:padding="10dp"/>
    </TableRow>
    <TableRow
        android:id="@+id/row2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <EditText
            android:inputType="text"
            android:id="@+id/editUsername"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_weight="2"
            android:singleLine="true"
            android:hint="input field2"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="2dip"
            android:layout_marginRight="75dp"   
            android:padding="10dp"/>
    </TableRow>             
      <TableRow
        android:id="@+id/row2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <EditText
            android:inputType="text"
            android:id="@+id/editUsername"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_weight="2"
            android:singleLine="true"
            android:hint="input field3"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="2dip"
            android:layout_marginRight="75dp"   
            android:padding="10dp"/>
    </TableRow>    
      <TableRow
        android:id="@+id/row2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <EditText
            android:inputType="text"
            android:id="@+id/editUsername"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_weight="2"
            android:singleLine="true"
            android:hint="input field4"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="2dip"
            android:layout_marginRight="75dp"   
            android:padding="10dp"/>
    </TableRow>
      <RelativeLayout
          android:id="@+id/linearlayout"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:orientation="horizontal" 
          android:gravity="bottom" >
           <Button
            android:id="@+id/takephoto"
            android:layout_width="110dp"
            android:layout_height="35dp"
            android:text="takephoto"                   
            android:textStyle="bold"
            android:layout_marginLeft="102dp"    />
      </RelativeLayout>
    

    【讨论】:

    • 谢谢@NagarjunaReddy,但是这个只有一个文本,在每个选项卡中,我都看过这个,但是这些示例都没有解释如何在每个选项卡下使用布局,例如,我想要一个表格布局的文本。
    • 将布局添加到您为每个选项卡创建的类中。选项卡将在每个活动中可见。
    • @shubhangi 非常感谢您的回复,您能给我一个简单的例子来说明如何在课堂上添加布局吗?我只知道如何在xml文件中使用布局,不知道如何在类中使用布局?
    • @NagarjunaReddy 能否给我一行简单的代码来说明如何在课堂上添加布局?或者也许引导我去 asimilatr 例子?非常感谢
    • @user1163454 您好,首先下载该代码并查看我的编辑代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 2015-09-06
    • 2015-04-27
    相关资源
    最近更新 更多