Android:如何将RelativeLayout 填充到整个屏幕宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21600888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:48:25  来源:igfitidea点击:

Android: How to fill RelativeLayout to full width of screen?

androidxmlandroid-layoutlayout

提问by MrMartinos

I am creating an Android app which has a main RelativeLayout and some LinearLayout within it.

我正在创建一个 Android 应用程序,其中有一个主要的 RelativeLayout 和一些 LinearLayout 。

Now i have this problem. When dragging items to the editor, e.g. a LinearLayout, it doesn't fit to the full width of the screen. How can I make this possible? This is my XML:

现在我有这个问题。将项目拖动到编辑器时,例如 LinearLayout,它不适合屏幕的整个宽度。我怎样才能做到这一点?这是我的 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:gravity="fill"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/itemDetailLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" 
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/itemImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="40dp"
            android:minWidth="40dp"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/itemName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="Name"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:fillViewport="true" >

        <TextView
            android:id="@+id/itemRecentHigh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Recent High" />

        <TextView
            android:id="@+id/itemRecentLow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Recent Low" />

        <TextView
            android:id="@+id/itemAverage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Average" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/listViewLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/itemDetailLayout"
    android:layout_marginTop="10dp"
    android:fillViewport="true"
    android:gravity="fill_horizontal"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/searchListView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true" >

    </ListView>
</LinearLayout>

Added a screenshot for more info. Added blue background to have some contrast.

添加了屏幕截图以获取更多信息。添加了蓝色背景以增加对比度。

http://i.stack.imgur.com/tPxsE.png

http://i.stack.imgur.com/tPxsE.png

采纳答案by M D

Replace this:

替换这个:

   android:layout_width="wrap_content"
   android:layout_height="wrap_content"

With

   android:layout_width="match_parent"
   android:layout_height="wrap_content"

into your LinearLayoutor Remove all the Padding from main RelativeLayout

进入您的LinearLayout或从主删除所有填充RelativeLayout

回答by Holyangel

Removing padding makes the linear layout fit the entire screen of the device

去除填充使线性布局适合设备的整个屏幕

回答by Isaac Urbina

Don't use fill_parent as it is deprecated. Instead to your RelativeLayout set

不要使用 fill_parent,因为它已被弃用。而不是你的 RelativeLayout 集

android:layout_width="fill_parent"
android:layout_height="fill_parent"

Then, to the list view that you want to fill the rest of the screen, you can set this tags

然后,对于要填充屏幕其余部分的列表视图,您可以设置此标签

android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"

By doing so, you are telling the XML that you want your ListView to do what fill_parent used to do in previous versions of Android.

通过这样做,您是在告诉 XML 您希望 ListView 执行之前版本的 Android 中 fill_parent 所执行的操作。

Kind regards!

亲切的问候!

回答by laalto

The space between the blue part and the "end of the screen". On the sides and above the part where the back button and home button are.

蓝色部分和“屏幕末端”之间的空间。在返回按钮和主页按钮所在的部分的侧面和上方。

Remove the padding from the root RelativeLayout.

从根删除填充RelativeLayout

回答by EdmDroid

Try

尝试

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical>
   // Other views within the linear layout
</LinearLayout>

I suppose you are using Eclipse. Personally, I hate the Graphical Editor of Eclipse and I usually write the XML code directly because working with the editor is a real pain

我想你正在使用 Eclipse。就我个人而言,我讨厌 Eclipse 的图形编辑器,我通常直接编写 XML 代码,因为使用编辑器真的很痛苦

回答by EdmDroid

you should try something like the following :

您应该尝试以下操作:

<LinearLayout
    android:id="@+id/itemDetailLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
     >

...


</LinearLayout>

what i mean you should do this :

我的意思是你应该这样做:

android:layout_width="fill_parent"

for every LinearLayout that you want its width to fill_parent .

对于您希望其宽度为 fill_parent 的每个 LinearLayout 。

FILL_PARENT means that the view wants to be as big as its parent as mentioned in the documentation.

FILL_PARENT 意味着视图想要和文档中提到的它的父视图一样大。

and please give me some feedback

请给我一些反馈

Hope That Helps .

希望有帮助。