相对布局中的百分比宽度

编程入门 行业动态 更新时间:2024-10-26 18:27:32
本文介绍了相对布局中的百分比宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为我的 Android 应用程序中的登录 Activity 设计表单布局.下面的图片是我想要的样子:

I am working on a form layout for a Login Activity in my Android App. The image below is how I want it to look like:

我能够使用以下 XML 实现此布局.问题是,它有点hackish.我必须为主机 EditText 硬编码宽度.具体来说,我必须指定:

I was able to achieve this layout with the following XML. The problem is, it's a bit hackish. I had to hard-code a width for the host EditText. Specifically, I had to specify:

android:layout_width="172dp"

我真的很想为主机和端口 EditText 的 .(类似于主机的 80%,端口的 20%.)这可能吗?以下 XML 适用于我的 Droid,但似乎不适用于所有屏幕.我真的想要一个更强大的解决方案.

I'd really like to give a percentage width to the host and port EditText's . (Something like 80% for the host, 20% for the port.) Is this possible? The following XML works on my Droid, but it doesn't seem to work for all screens. I would really like a more robust solution.

<RelativeLayout xmlns:android="schemas.android/apk/res/android" android:id="@+id/main" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/host_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/home" android:paddingLeft="15dp" android:paddingTop="0dp" android:text="host" android:textColor="#a5d4e2" android:textSize="25sp" android:textStyle="normal" /> <TextView android:id="@+id/port_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/home" android:layout_toRightOf="@+id/host_input" android:paddingTop="0dp" android:text="port" android:textColor="#a5d4e2" android:textSize="25sp" android:textStyle="normal" /> <EditText android:id="@+id/host_input" android:layout_width="172dp" android:layout_height="wrap_content" android:layout_below="@id/host_label" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="4dp" android:background="@android:drawable/editbox_background" android:inputType="textEmailAddress" /> <EditText android:id="@+id/port_input" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_below="@id/host_label" android:layout_marginTop="4dp" android:layout_toRightOf="@id/host_input" android:background="@android:drawable/editbox_background" android:inputType="number" /> <TextView android:id="@+id/username_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/host_input" android:paddingLeft="15dp" android:paddingTop="15dp" android:text="username" android:textColor="#a5d4e2" android:textSize="25sp" android:textStyle="normal" /> <EditText android:id="@+id/username_input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/username_label" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="4dp" android:background="@android:drawable/editbox_background" android:inputType="textEmailAddress" /> <TextView android:id="@+id/password_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/username_input" android:paddingLeft="15dp" android:paddingTop="15dp" android:text="password" android:textColor="#a5d4e2" android:textSize="25sp" android:textStyle="normal" /> <EditText android:id="@+id/password_input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/password_label" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="4dp" android:background="@android:drawable/editbox_background" android:inputType="textPassword" /> <ImageView android:id="@+id/home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_centerVertical="false" android:paddingLeft="15dp" android:paddingRight="15dp" android:paddingTop="15dp" android:scaleType="fitStart" android:src="@drawable/home" /> <Button android:id="@+id/login_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/password_input" android:layout_marginLeft="15dp" android:layout_marginTop="15dp" android:text=" login " android:textSize="18sp" > </Button> </RelativeLayout>

推荐答案

您正在寻找 android:layout_weight 属性.它将允许您使用百分比来定义您的布局.

You are looking for the android:layout_weight attribute. It will allow you to use percentages to define your layout.

在以下示例中,左按钮使用 70% 的空间,右按钮使用 30%.

In the following example, the left button uses 70% of the space, and the right button 30%.

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:text="left" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".70" /> <Button android:text="right" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".30" /> </LinearLayout>

它适用于任何类型的视图,您可以用一些 EditText 替换按钮以满足您的需要.

It works the same with any kind of View, you can replace the buttons with some EditText to fit your needs.

请务必将 layout_width 设置为 0dp 否则您的视图可能无法正确缩放.

Be sure to set the layout_width to 0dp or your views may not be scaled properly.

请注意,权重总和不必等于 1,我只是觉得这样更容易阅读.您可以将第一个权重设置为 7,将第二个权重设置为 3,这将得到相同的结果.

Note that the weight sum doesn't have to equal 1, I just find it easier to read like this. You can set the first weight to 7 and the second to 3 and it will give the same result.

更多推荐

相对布局中的百分比宽度

本文发布于:2023-10-07 23:41:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:百分比   宽度   布局

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!