【问题标题】:How to set different Layouts in 1 XML file in Android如何在 Android 的 1 个 XML 文件中设置不同的布局
【发布时间】:2010-10-27 04:27:28
【问题描述】:

嗨朋友们,我已经在 LinearLayout 中通过 TabLayout 设置了我的布局,但最终我的按钮仍然无法正确设置,因为 TabLayout 将所有字段划分为列数,因此任何人都可以帮助如何在一个 XML 文件中设置 2 个布局 表示 TabLayout 中的所有字段以及线性布局中的 Login 和 Register 按钮或其他一些字段,以便它们可以正确设置。

提前致谢。

布局

用户名!编辑文本框

密码!编辑文本框

登录!注册

按钮!按钮

我希望我的布局采用上述格式,所以我使用了 TabLaout,但在这种情况下,它会导致我的按钮视图以及它拉伸一个按钮而不是另一个按钮,因为 EditText 大于 TextView

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:textSize="26sp"
    android:text="@string/login_text"/>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
    <TableRow>
    <TextView  
        android:layout_width="120px"  
        android:text="User name"
        />
    <EditText
        android:id="@+id/txtUserName"
        android:singleLine="true"
        android:maxLength="20"
        android:width="195px"
    />
    </TableRow>
    <TableRow>
    <TextView  
        android:layout_width="120px"  
        android:text="Password"
        />
    <EditText
        android:id="@+id/txtPassword"
        android:width="195px"
        android:maxLength="20"
        android:singleLine="true"
        android:password="true"
    />
    </TableRow>
    <TableRow>
            <TextView />
            <CheckBox android:id="@+id/chkRememberPassword"
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:text="Remember Password"
                />   
        </TableRow>
        <TableRow>
        <Button 
                android:id="@+id/buttonRegister" 
                android:text="Register" 
                android:layout_width="124px"
                android:layout_height="wrap_content"
                android:layout_below="@+id/chkRememberPassword"
                android:layout_alignRight="@+id/chkRememberPassword"
                />
        <Button 
            android:id="@+id/buttonSignIn" 
            android:text="Log In" 
            android:layout_width="124px"
            android:layout_height="wrap_content"
            android:layout_below="@+id/chkRememberPassword"
            android:layout_alignLeft="@+id/chkRememberPassword"
            />        
    </TableRow>

    <TextView  
    android:id="@+id/statusError"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     />
    </TableLayout>
    </LinearLayout>

【问题讨论】:

  • 请正确格式化您的代码。选择它并按 CTRL+K。
  • 如果可能的话,把你的问题直观地提到,我的意思是粗略的草图或类似的东西,这样你就会得到准确的答案

标签: android android-layout android-2.2-froyo android-xml


【解决方案1】:

我建议你在 TableLayout 中使用 wrap_content,然后你可以在 LinearLayout 中使用按钮

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>

【讨论】:

    【解决方案2】:

    不可能在一个 xml 文件中设置多个布局...出于同样的原因,Android 具有布局、布局-横向等类型的文件夹系统..

    你可以做的就像 100rabh 解释的那样...通过应用相对布局和填充父设置来使布局通用...

    否则,正如我解释的那样,您需要制作 2 个 xml 布局文件并将横向布局放置在 layout-landscape 文件夹中,从而根据手机的方向自动设置相应的布局......

    希望对你有帮助...

    【讨论】:

      【解决方案3】:

      您需要创建 2 个 XML 文件:

      1. 普通layout文件夹中的一个
      2. res/layout-land 中的第二个用于定义横向布局。如果res/layout-land 文件夹不存在,您需要创建它。

      【讨论】:

        猜你喜欢
        • 2020-04-06
        • 1970-01-01
        • 2018-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多