[英]Splitting Android Layout Vertically Adding Gravity To Both Sides To Center Objects
I have an android layout I'd like to split down the middle vertically and then add gravity so I can center items within the left and the right halves of the layout. 我有一个Android布局,我想垂直分开中间部分,然后增加重力,这样我就可以在布局的左右两半内使项目居中。
Can someone explain or post an example of how this might be accomplished? 有人可以解释或发布如何实现此目标的示例吗?
Thus far I've created a layout and a horizontal layout. 到目前为止,我已经创建了一个布局和一个水平布局。 My problem is the horizontal layout needs balance so I'd like to figure out a way to change my current layout (show below) to include a way of centering the objects within the left and right halves of the screen. 我的问题是水平布局需要平衡,因此我想找出一种更改当前布局的方法(如下所示),以包括一种将对象在屏幕左右两边居中的方法。
SOURCE: 资源:
<?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"
android:background="@drawable/background"
android:orientation="vertical" >
<ImageView
android:id="@+id/emblem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:gravity="center"
android:scaleType="fitStart"
android:src="@drawable/apn_app_logo" />
<Button
android:id="@+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/emblem"
android:layout_alignParentRight="true"
android:layout_marginBottom="23dp"
android:layout_marginRight="22dp"
android:background="@drawable/apn_app_go_button" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/go_button"
android:gravity="center"
android:text="@string/start_text2"
android:textColor="#000000"
android:textSize="18sp" />
</RelativeLayout>
First of all android:orientation
attribute will not work in Relative Layout
.If you want to split the screen half into two equal layout your parent layout will be a linear layout
with android:orientation= horizontal
. 首先android:orientation
属性在Relative Layout
不起作用。如果要将屏幕分成两个相等的布局,则父布局将是android:orientation= horizontal
的linear layout
。 Then inside that have 2 LinearLayout
with each android:orientation= vertical
. 然后在里面有2个LinearLayout
,每个android:orientation= vertical
。 Inside 1st Linear layout
you can have the ImageView
, Button
and TextView
with each of their layout_gravity=center
. 在1st Linear layout
您可以具有ImageView
, Button
和TextView
以及各自的layout_gravity=center
。
Hope this helps. 希望这可以帮助。 If you want to do something in the 2nd half of teh screen , do all teh stuffs in the 2nd LinearLayout. 如果您想在屏幕的第二部分做某事,请在第二LinearLayout中进行所有操作。 Happy Coding. 编码愉快。
Here's some code which should be self-explanotory: 这是一些应该自解释的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#00dd00"
android:gravity="center"
>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#dd0000"
android:gravity="center_horizontal"
>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
and here's what you'll achieve: 这是您将实现的目标:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.