简体   繁体   English

如何使多个元素在Android中水平填充屏幕

[英]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>

I get this result : 我得到这个结果: 在此处输入图片说明

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 get this : 我得到这个: 在此处输入图片说明

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.

相关问题 如何使UI元素充满整个Android屏幕 - How do I make UI elements fill the entire Android Screen Android 独特的闪屏:如何让它满屏? - Android unique splash screen : how to make it fill screen? Android:ScrollView 包含两个嵌套的 ConstraintLayouts,无法水平填充屏幕 - Android: ScrollView containing two nested ConstraintLayouts, cant fill screen horizontally 使gridview水平填充 - Make gridview fill horizontally 如何使带有多个子布局的RelativeLayout的ScrollView充满屏幕? - How to make ScrollView with a RelativeLayout with multiple child layouts fill the screen? android:如何打开通话屏幕并用数字填充但不拨打电话? - android: How to open the call screen and fill it with a number but do not make a call? android:layout:如何让我的对话框用XML填充屏幕 - android: layout: How to make my dialog box fill the screen with the XML 如何在Android浏览器中使HTML填充WebView上的全屏高度 - How to make html to fill full screen height on webview in android browser Android-如何使视图仅填充屏幕的其余部分 - Android -how to make view fill only the rest of the screen only 如何使按钮填充Android屏幕宽度的50%? - How to make button to fill 50% from the screen width in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM