Android:如何填充相对布局到全屏?

时间:2022-05-08 21:11:15

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

我正在创建一个Android应用,它有一个主要的相对布局和一些线性布局。

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:

现在我有了这个问题。拖拽编辑器时,例如线性布局,它不适合屏幕的全宽。我怎样才能使这成为可能?这是我的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.

增加了一个屏幕截图来获取更多信息。添加蓝色背景有一些对比。

Android:如何填充相对布局到全屏?

6 个解决方案

#1


16  

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 LinearLayout or Remove all the Padding from main RelativeLayout

在你的线性布局或删除所有的填充从主相对论布局

#2


3  

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

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

#3


1  

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.

蓝色部分与“屏幕末端”之间的空间。在背面和上面的部分,后退按钮和home按钮是。

Remove the padding from the root RelativeLayout.

从根相对布局中删除填充。

#4


1  

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代码,因为与编辑器一起工作真的很痛苦

#5


0  

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的每个线性布局。

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 .

希望有帮助。

#6


0  

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

不要使用fill_parent,因为它被弃用。而不是你的相对布局

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执行fill_parent在以前的Android版本中所做的操作。

Kind regards!

亲切的问候!

#1


16  

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 LinearLayout or Remove all the Padding from main RelativeLayout

在你的线性布局或删除所有的填充从主相对论布局

#2


3  

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

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

#3


1  

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.

蓝色部分与“屏幕末端”之间的空间。在背面和上面的部分,后退按钮和home按钮是。

Remove the padding from the root RelativeLayout.

从根相对布局中删除填充。

#4


1  

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代码,因为与编辑器一起工作真的很痛苦

#5


0  

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的每个线性布局。

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 .

希望有帮助。

#6


0  

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

不要使用fill_parent,因为它被弃用。而不是你的相对布局

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执行fill_parent在以前的Android版本中所做的操作。

Kind regards!

亲切的问候!