Android 如何将编辑和按钮置于父视图的中心
以下是我的xml:Android 如何将编辑和按钮置于父视图的中心,android,xml,Android,Xml,以下是我的xml: <RelativeLayout android:id="@+id/RelativeLayout01" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:orientation="horizontal" android:lay
<RelativeLayout android:id="@+id/RelativeLayout01"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<EditText android:id="@+id/edit_message"
android:layout_weight="5"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_search"
android:onClick="searchUsingTag" />
</LinearLayout>
</RelativeLayout>
但这并不是我想要的。我想要谷歌搜索之类的东西。有一个长编辑框,然后是一个小按钮
如果要使用线性布局中的
布局权重属性,必须将此布局设置为与其父布局匹配:
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
...
...
然后,你可以试着玩重量游戏,为孩子们设定不同的尺寸。
另外:在您的示例中,您必须为每个子项设置权重,在父项外部按下按钮的textView,因为该按钮没有设置权重。更改线性布局的宽度以填充父项,并尝试权重,直到获得所需的效果
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:weightSum="5" >
<EditText
android:id="@+id/edit_message"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="4" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="searchUsingTag"
android:text="@string/button_search" />
</LinearLayout>
</RelativeLayout>
正如我在评论中所说,您需要设置:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:weightSum="5" >
<EditText
android:id="@+id/edit_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:weightSum="5" >
<Button
android:id="@+id/Button01"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="searchUsingTag"
android:text="@string/button_search" />
</LinearLayout>
</RelativeLayout>
将每个元素划分为不同的布局。另外,请记住添加所有id(您忘记添加按钮id)并将EditText
宽度设置为fill\u parent
,然后您会得到一个EditText
类似“谷歌”的搜索字段或设置您自己的大小
建议
就个人而言,我更喜欢使用LinearLayout
作为主布局,而不是使用RelativeLayout
。这只是一个建议。如果你喜欢相对的,没关系。将每个元素放入线性布局中不同的线性布局中。然后,将文本元素的宽度设置为fill\u parent
,它们位于视图的顶部。我已经编辑了代码。我以为你在相对布局中包含了线性布局。编辑后的代码应该可以工作。