[英]How to make multiple elements fill screen horizontally in Android
I want to have an edittext and button fill screen horizontally. 我想水平放置一个edittext和按钮填充屏幕。 When I have this configuration :
当我有此配置时:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/et_word"
android:padding="10dp"
/>
<requestFocus/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/btn_word"
android:text="@string/send"
android:padding="10dp"
/>
</LinearLayout>
When I have this configuration : 当我有此配置时:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/et_word"
android:padding="10dp"
/>
<requestFocus/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/btn_word"
android:text="@string/send"
android:padding="10dp"
/>
</LinearLayout>
I guess in second configuration, edittext really fills parent but button is not visible. 我猜在第二种配置中,edittext确实填充了父级,但是按钮不可见。 How can I change my code so that both edittext and button would fill screen horizontally?
如何更改代码,以便edittext和button都可以水平填充屏幕? Thanks.
谢谢。
You can use layout weight to do this. 您可以使用布局权重执行此操作。 For example, if you want the button to be wrap_content, and have the EditText fill the remaining space, do this:
例如,如果您希望按钮为wrap_content,并让EditText填充剩余空间,请执行以下操作:
<LinearLayout>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
If you want them both to take up half the layout, you can use layout_weight="1"
for each. 如果希望它们都占用一半的布局,则可以为每个使用
layout_weight="1"
。 Or set one to 1 and the other to 2 to have a one-third/two-third layout. 或将一个设置为1,将另一个设置为2,以具有三分之一/三分之二的布局。 Whatever you need.
无论你需要什么。
See this answer by Flo for more information on the layout_weight attribute. 有关layout_weight属性的更多信息,请参见Flo的答案。
You can use the attribute Weight which kind of give a relative size to widget. 您可以使用权重属性,它为小部件提供相对大小。 Here is an example:
这是一个例子:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="40"
android:gravity="center"
android:id="@+id/et_word"
android:padding="10dp"
/>
<requestFocus/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:gravity="center"
android:id="@+id/btn_word"
android:text="@string/send"
android:padding="10dp"
/>
</LinearLayout>
With that code, yout Edittext will have a size of 4 time the size of the button, independantly of the size of your width 使用该代码,您的Edittext的大小将是按钮大小的4倍,而与宽度的大小无关
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.