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

发布于 2024-09-29 05:30:04 字数 3003 浏览 1 评论 0原文

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

提前致谢。

布局

用户名 !编辑文本

框密码!编辑文本框

登录!注册

按钮!按钮

我希望我的布局采用上述格式,因此我使用了 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>

Hi friends i have set my layout thru TabLayout within LinearLayout but still my button in the end couldn't be set properly as TabLayout divides all the fields in number of columns so can anyone help how to set 2 Layouts in one XML file
means all the fields in TabLayout and remaining both Login and Register buttons in Linear Layout ore some other so that they can set properly.

Thanks in advance.

Layout

Username ! EditTextBox

Password ! EditTextBox

Login ! Register

Button ! Button

I want my Layout in above Format so i have used TabLaout but in that case it is causing my button's view as well as it is stretching one button than other one as EditText is larger than 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悟红尘 2024-10-06 05:30:04

我建议你在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"
>

I suggest you to use wrap_content in TableLayout & then you can have your buttons in LinearLayout

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>
原谅过去的我 2024-10-06 05:30:04

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

您可以做的正如 100rabh 所解释的...通过应用相对布局并填充父设置来使布局通用...

否则,正如我所解释的,您需要制作 2 个 xml 布局文件,并将横向布局放置在布局-横向文件夹中,从而将根据手机方向...

希望这对您有帮助...

Its not possible to set more than one layout in a single xml file... And for the same reason Android has layout, layout-landscape, etc. type of folder system..

What you can do is as explained by 100rabh... Make the layout universal by applying relative layout and fill parent settings...

Otherwise as i explained you need to make 2 xml layout files and place the layout for landscape in the layout-landscape folder thereby the respective layout will automatically be set according to the orientation of the phone...

Hope this helps u...

我的黑色迷你裙 2024-10-06 05:30:04

您需要创建 2 个 XML 文件:

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

You need to create 2 XML files:

  1. One in the normal layout folder
  2. A second in a res/layout-land to define the layout for the landscape orientation. You'll need to create the res/layout-land folder if it does not already exist.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文