我对Android不太了解,正在尝试创建一个示例应用程序,其中3个元素(2个按钮和1个文本视图)水平放置。我需要文本在中心,两个按钮分别在视图的左侧和右侧对齐。
例如:
|<Button> <Text> <Button>|
希望您能帮我解决这个问题。
提前致谢。
我对Android不太了解,正在尝试创建一个示例应用程序,其中3个元素(2个按钮和1个文本视图)水平放置。我需要文本在中心,两个按钮分别在视图的左侧和右侧对齐。
例如:
|<Button> <Text> <Button>|
希望您能帮我解决这个问题。
提前致谢。
编辑:我相信这个代码将解决你的问题。请看下面的代码。如果有帮助,请让我知道!我知道这对我很有帮助,我会使用它的。如果有效,请接受此答案!=)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:id="@+id/button1button"></Button>
<EditText
android:layout_height="wrap_content"
android:id="@+id/firstedittext"
android:layout_width="wrap_content"></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
android:id="@+id/button2button"></Button>
</TableRow>
</TableLayout>
编辑:添加了相对布局,看看是否适用于你,如果有问题请告诉我。你需要调整“10px”以获得所需的距离。我正在尝试更好的方法,使距离更加动态化。
你可以将按钮设置为“alignParentLeft”和“alignParentRight”,使它们与屏幕的左侧和右侧对齐。然而,我仍然无法解决两个视图之间的文本问题。
你可以在布局的xml文件中实现这一点。首先将其设为水平布局,然后添加第一个按钮,接着是文本视图,最后是第二个按钮。稍后会发布一个示例xml。
<RelativeLayout
android:id="@+id/LinearLayout01"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:layout_alignParentLeft="true"
android:layout_marginRight="10px"
android:id="@+id/button1button"></Button>
<EditText
android:layout_height="wrap_content"
android:id="@+id/firstedittext"
android:layout_toRightOf="@id/button1button"
android:layout_marginRight="10px"
android:layout_width="wrap_content"></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/firstedittext"
android:text="button2"
android:id="@+id/button2button"></Button>
</RelativeLayout>
这将解决问题。您需要的是绝对重力。这个应该适用于所有Android屏幕大小。我认为您不需要使用任何边距或填充,一旦您的子元素中有内容,它们就会使您混乱(在选择边距时,边距是相对的,即:当您的文本较少时,您希望较小的边距......这经常发生在翻译、新功能、替换等情况下。)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:layout_alignParentLeft="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/firstedittext"
android:layout_centerInParent="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
android:layout_alignParentRight="true"/>
</RelativeLayout>
这是我的 LinearLayout 代码。希望能对你有所帮助!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="Button1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:text="Chao cac ban" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="Button2" />
</LinearLayout>
</LinearLayout>