Android 使用relativelayout对齐中心对齐的两行控件
你好, 我有一个带有两列EditText的relativeLayout 我希望第二行从屏幕中央开始,第一列中的EditText从左侧开始,在中间停止 我一直在摆弄centerInParent和CenterHorizontal toLeftOf,但看不出是否正确。我不想使用LinearLayout,如果可能的话,我只希望使用RelativeLayout。所以线性布局不是我的选择 非常感谢你的建议。 截图: 我使用的样式:Android 使用relativelayout对齐中心对齐的两行控件,android,android-relativelayout,Android,Android Relativelayout,你好, 我有一个带有两列EditText的relativeLayout 我希望第二行从屏幕中央开始,第一列中的EditText从左侧开始,在中间停止 我一直在摆弄centerInParent和CenterHorizontal toLeftOf,但看不出是否正确。我不想使用LinearLayout,如果可能的话,我只希望使用RelativeLayout。所以线性布局不是我的选择 非常感谢你的建议。 截图: 我使用的样式: Android Studio 0.5.4 包装内容 包装内容 22便士
Android Studio 0.5.4
包装内容
包装内容
22便士
4dp
布局:
<resources>
<style name="addressBookLand">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">22sp</item>
<item name="android:layout_marginBottom">4dp</item>
</style>
</resources>
如果您肯定不想使用
线性布局
,那么一个优雅的解决方案可能是创建一个宽度和高度为0的文本视图
,作为中心点的标记,并使用它来定位周围的其他视图。比如,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fffc">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:gravity="center"
android:inputType="text"
android:text="Address Book App"
android:textStyle="bold|italic" />
<EditText
android:id="@+id/etFirstName"
style="@style/addressBookLand"
android:hint="Enter your first name"
android:inputType="text"
android:layout_below="@id/tvTitle"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/etLastName"/>
<EditText
android:id="@+id/etLastName"
style="@style/addressBookLand"
android:hint="Enter your last name"
android:inputType="text"
android:layout_below="@id/tvTitle"
android:layout_centerHorizontal="true"/>
<EditText
android:id="@+id/etAddressLine"
style="@style/addressBookLand"
android:hint="Enter your address"
android:inputType="text"
android:layout_below="@id/etFirstName"/>
<EditText
android:id="@+id/etDistrict"
style="@style/addressBookLand"
android:hint="Enter your district"
android:inputType="text"
android:layout_below="@id/etLastName"
android:layout_toRightOf="@id/etAddressLine"/>
<EditText
android:id="@+id/etCity"
style="@style/addressBookLand"
android:hint="Enter your city"
android:inputType="text"
android:layout_below="@id/etAddressLine"/>
<EditText
android:id="@+id/etPostCode"
style="@style/addressBookLand"
android:hint="Enter your postcode"
android:inputType="number"
android:layout_below="@id/etDistrict"
android:layout_toRightOf="@id/etCity"/>
<EditText
android:id="@+id/etPhoneNumber"
style="@style/addressBookLand"
android:hint="Enter your phone number"
android:inputType="phone"
android:layout_below="@id/etCity"/>
<EditText
android:id="@+id/etEmailAddress"
style="@style/addressBookLand"
android:hint="Enter your email Address"
android:inputType="textEmailAddress"
android:layout_below="@id/etPostCode"
android:layout_toRightOf="@id/etPhoneNumber"/>
<Button
android:id="@+id/btnSubmit"
style="@style/addressBookLand"
android:text="Submit"
android:layout_below="@id/etPhoneNumber"
android:layout_toLeftOf="@id/btnCancel"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/btnCancel"
style="@style/addressBookLand"
android:onClick="onClickCancel"
android:text="Cancel"
android:layout_below="@id/etEmailAddress"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<TextView
android:id="@+id/centre_marker"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"/>
或者,正如其他人所提到的,水平线性布局
中的两个大小相等的垂直线性布局
可以得到您需要的结果,只要您不打算使布局更复杂或更不规整
我希望这有帮助。如果你需要澄清,你可以给我留言,我会尽可能地解释。如果你肯定不想使用线性布局
,那么一个优雅的解决方案可能是创建一个宽度和高度为0的文本视图
,作为中心点的标记,并用它来定位其他视图
。比如,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fffc">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:gravity="center"
android:inputType="text"
android:text="Address Book App"
android:textStyle="bold|italic" />
<EditText
android:id="@+id/etFirstName"
style="@style/addressBookLand"
android:hint="Enter your first name"
android:inputType="text"
android:layout_below="@id/tvTitle"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/etLastName"/>
<EditText
android:id="@+id/etLastName"
style="@style/addressBookLand"
android:hint="Enter your last name"
android:inputType="text"
android:layout_below="@id/tvTitle"
android:layout_centerHorizontal="true"/>
<EditText
android:id="@+id/etAddressLine"
style="@style/addressBookLand"
android:hint="Enter your address"
android:inputType="text"
android:layout_below="@id/etFirstName"/>
<EditText
android:id="@+id/etDistrict"
style="@style/addressBookLand"
android:hint="Enter your district"
android:inputType="text"
android:layout_below="@id/etLastName"
android:layout_toRightOf="@id/etAddressLine"/>
<EditText
android:id="@+id/etCity"
style="@style/addressBookLand"
android:hint="Enter your city"
android:inputType="text"
android:layout_below="@id/etAddressLine"/>
<EditText
android:id="@+id/etPostCode"
style="@style/addressBookLand"
android:hint="Enter your postcode"
android:inputType="number"
android:layout_below="@id/etDistrict"
android:layout_toRightOf="@id/etCity"/>
<EditText
android:id="@+id/etPhoneNumber"
style="@style/addressBookLand"
android:hint="Enter your phone number"
android:inputType="phone"
android:layout_below="@id/etCity"/>
<EditText
android:id="@+id/etEmailAddress"
style="@style/addressBookLand"
android:hint="Enter your email Address"
android:inputType="textEmailAddress"
android:layout_below="@id/etPostCode"
android:layout_toRightOf="@id/etPhoneNumber"/>
<Button
android:id="@+id/btnSubmit"
style="@style/addressBookLand"
android:text="Submit"
android:layout_below="@id/etPhoneNumber"
android:layout_toLeftOf="@id/btnCancel"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/btnCancel"
style="@style/addressBookLand"
android:onClick="onClickCancel"
android:text="Cancel"
android:layout_below="@id/etEmailAddress"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<TextView
android:id="@+id/centre_marker"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"/>
或者,正如其他人所提到的,水平线性布局
中的两个大小相等的垂直线性布局
可以得到您需要的结果,只要您不打算使布局更复杂或更不规整
我希望这有帮助。如果您需要澄清,您可以给我留言,我会尽可能地解释。将编辑文本分成两个垂直线性布局。将水平线性布局环绕在这些布局周围,并为两个垂直线性布局赋予权重=“1”
将编辑文本分组为两个垂直线性布局。将水平线性布局环绕在这些布局周围,并为两个垂直线性布局赋予权重=“1”
只需使用2个放置在线性布局中的相对布局创建柱,并在列上使用宽度设置为匹配父列的权重。
请记住,您可以将布局嵌套在其他布局中。只需使用两个放置在线性布局中的相关布局创建列,并在列上使用宽度设置为与父列匹配的权重即可。
请记住,您可以在其他布局中嵌套布局。尝试以下代码
<LinearLayout
android:orientation="horizontal"
android:width="match_parent"
android:height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:weight="1"
android:width="0dp"
android:height="wrap_content">
<!-- put first column here -->
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:weight="1"
android:width="0dp"
android:height="wrap_content">
<!-- put second column here -->
</LinearLayout
</LinearLayout
希望这能解决您的问题尝试以下代码
<LinearLayout
android:orientation="horizontal"
android:width="match_parent"
android:height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:weight="1"
android:width="0dp"
android:height="wrap_content">
<!-- put first column here -->
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:weight="1"
android:width="0dp"
android:height="wrap_content">
<!-- put second column here -->
</LinearLayout
</LinearLayout
希望这能解决您的问题尝试使用以下代码。希望这能奏效
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fffc" >
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="text"
android:text="Address Book App"
android:textSize="22sp"
android:textStyle="bold|italic" />
<EditText
android:id="@+id/etFirstName"
style="@style/addressBookLand"
android:layout_alignParentLeft="true"
android:layout_below="@id/tvTitle"
android:layout_toLeftOf="@id/etLastName"
android:hint="Enter your first name"
android:inputType="text" />
<EditText
android:id="@+id/etAddressLine"
style="@style/addressBookLand"
android:layout_below="@id/etFirstName"
android:hint="Enter your address"
android:inputType="text" />
<EditText
android:id="@+id/etCity"
style="@style/addressBookLand"
android:layout_below="@id/etAddressLine"
android:hint="Enter your city"
android:inputType="text" />
<Button
android:id="@+id/btnCancel"
style="@style/addressBookLand"
android:layout_below="@id/etEmailAddress"
android:layout_centerHorizontal="true"
android:onClick="onClickCancel"
android:text="Cancel" />
<Button
android:id="@+id/btnSubmit"
style="@style/addressBookLand"
android:layout_alignParentLeft="true"
android:layout_below="@id/etPhoneNumber"
android:layout_toLeftOf="@id/btnCancel"
android:text="Submit" />
<EditText
android:id="@+id/etLastName"
style="@style/addressBookLand"
android:layout_alignBaseline="@+id/etFirstName"
android:layout_alignBottom="@+id/etFirstName"
android:layout_alignParentRight="true"
android:ems="10"
android:hint="Enter your last name"
android:inputType="text" />
<EditText
android:id="@+id/etDistrict"
style="@style/addressBookLand"
android:layout_alignParentRight="true"
android:layout_below="@+id/etLastName"
android:ems="10"
android:hint="Enter your district"
android:inputType="text" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/etPostCode"
style="@style/addressBookLand"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Enter your postcode"
android:inputType="number" />
<EditText
android:id="@+id/etEmailAddress"
style="@style/addressBookLand"
android:layout_alignParentRight="true"
android:layout_below="@+id/etPostCode"
android:ems="10"
android:hint="Enter your email Address"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/etPhoneNumber"
style="@style/addressBookLand"
android:layout_alignParentLeft="true"
android:layout_below="@+id/etCity"
android:ems="10"
android:hint="Enter your phone number"
android:inputType="phone" />
尝试使用以下代码。希望这能奏效
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fffc" >
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:inputType="text"
android:text="Address Book App"
android:textSize="22sp"
android:textStyle="bold|italic" />
<EditText
android:id="@+id/etFirstName"
style="@style/addressBookLand"
android:layout_alignParentLeft="true"
android:layout_below="@id/tvTitle"
android:layout_toLeftOf="@id/etLastName"
android:hint="Enter your first name"
android:inputType="text" />
<EditText
android:id="@+id/etAddressLine"
style="@style/addressBookLand"
android:layout_below="@id/etFirstName"
android:hint="Enter your address"
android:inputType="text" />
<EditText
android:id="@+id/etCity"
style="@style/addressBookLand"
android:layout_below="@id/etAddressLine"
android:hint="Enter your city"
android:inputType="text" />
<Button
android:id="@+id/btnCancel"
style="@style/addressBookLand"
android:layout_below="@id/etEmailAddress"
android:layout_centerHorizontal="true"
android:onClick="onClickCancel"
android:text="Cancel" />
<Button
android:id="@+id/btnSubmit"
style="@style/addressBookLand"
android:layout_alignParentLeft="true"
android:layout_below="@id/etPhoneNumber"
android:layout_toLeftOf="@id/btnCancel"
android:text="Submit" />
<EditText
android:id="@+id/etLastName"
style="@style/addressBookLand"
android:layout_alignBaseline="@+id/etFirstName"
android:layout_alignBottom="@+id/etFirstName"
android:layout_alignParentRight="true"
android:ems="10"
android:hint="Enter your last name"
android:inputType="text" />
<EditText
android:id="@+id/etDistrict"
style="@style/addressBookLand"
android:layout_alignParentRight="true"
android:layout_below="@+id/etLastName"
android:ems="10"
android:hint="Enter your district"
android:inputType="text" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/etPostCode"
style="@style/addressBookLand"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Enter your postcode"
android:inputType="number" />
<EditText
android:id="@+id/etEmailAddress"
style="@style/addressBookLand"
android:layout_alignParentRight="true"
android:layout_below="@+id/etPostCode"
android:ems="10"
android:hint="Enter your email Address"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/etPhoneNumber"
style="@style/addressBookLand"
android:layout_alignParentLeft="true"
android:layout_below="@+id/etCity"
android:ems="10"
android:hint="Enter your phone number"
android:inputType="phone" />
你也说线性布局不是你的选择,你应该考虑把它作为相对布局的一个孩子使用!
这样,您可以围绕两个按钮设置水平线性布局,并将它们水平居中
因此,不是两个按钮,而是类似于:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:id="@+id/center"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/fist_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/center" />
<EditText
android:id="@+id/second_row"
android:layout_alignLeft="@+id/center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fist_row"/>
这也是一个解决方案:
你也说线性布局不是你的选择,你应该考虑把它作为相对布局的子使用!
这样,您可以围绕两个按钮设置水平线性布局,并将它们水平居中
因此,不是两个按钮,而是类似于:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:id="@+id/center"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/fist_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/center" />
<EditText
android:id="@+id/second_row"
android:layout_alignLeft="@+id/center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fist_row"/>
这也是一个解决方案:
您的描述不清楚。你能发布所需布局的图像吗?另外,为什么线性布局不是一个选项?您的描述不清楚。你能发布所需布局的图像吗?另外,为什么LinearLayout不是一个选项?我不喜欢嵌套布局解决方案,因为它只是使用更多的处理能力来渲染它们。我不喜欢嵌套布局解决方案,因为它只是使用更多的处理能力来渲染它们。我不喜欢嵌套布局解决方案,因为它只是使用更多的处理能力来渲染它们。在本文中,建议使用GridLayout而不是嵌套的线性布局来提高性能。也许这就是我们要走的路?您似乎正在寻找网格。我不喜欢嵌套布局解决方案,因为它只是使用更多的处理能力来渲染网格。在本文()中,建议使用GridLayout而不是嵌套的线性布局来提高性能。也许这就是我们要走的路?看起来网格就是你要找的。我不喜欢嵌套布局解决方案,因为它只是使用更多的处理能力来渲染网格。我不喜欢嵌套布局解决方案,因为它只是使用更多的处理能力来渲染网格。我正在考虑这样做。我要试一试。唯一的缺点是每行都需要一个中心视图。谢谢。你不应该每行都要一个记号笔。他们会排队的。如果他们和他们的父母在同一个高度,应该不会有任何区别。我正在考虑这样做。我要试一试。唯一的缺点是每行都需要一个中心视图。谢谢。你不应该每行都要一个记号笔。他们会排队的。如果他们在他们父母的同一高度,应该不会有任何区别。