如何在Android中的一个XML文件中设置不同的布局

3

大家好,我使用了LinearLayout中的TabLayout设置了我的布局,但是我的按钮在最后没有被正确地设置,因为TabLayout将所有的字段分成多列,所以有谁能帮忙解决如何在一个XML文件中设置2个布局,即TabLayout中的所有字段和剩余的登录和注册按钮都在LinearLayout或其他布局中,以便它们可以正确地设置。

提前感谢。

布局

用户名!编辑框

密码!编辑框

登录!注册

按钮!按钮

我希望我的布局以上面的格式呈现,因此我使用了TabLayout,但在这种情况下,它会拉伸我的按钮视图,因为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。 - EboMike
将以下与编程有关的内容从英语翻译成中文。仅返回已翻译的文本:如果可能的话,请在视觉上提供您的问题,例如粗略草图等,以便您获得确切的答案。 - Paresh Mayani
3个回答

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

0

在单个XML文件中设置多个布局是不可能的...出于同样的原因,Android有layout、layout-landscape等类型的文件夹系统。

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

否则,就像我所解释的那样,你需要制作2个XML布局文件,并将横向布局放置在layout-landscape文件夹中,这样相应的布局将根据手机的方向自动设置...

希望这能帮到你...


0

您需要创建2个XML文件:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接