简体   繁体   English

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

[英]Android: How to fill RelativeLayout to full width of screen?

I am creating an Android app which has a main RelativeLayout and some LinearLayout within it. 我正在创建一个具有主RelativeLayout和其中一些LinearLayout的Android应用。

Now i have this problem. 现在我有这个问题。 When dragging items to the editor, eg a LinearLayout, it doesn't fit to the full width of the screen. 将项目拖到编辑器(例如LinearLayout)时,它不适合屏幕的整个宽度。 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. 添加了蓝色背景以形成一些对比。

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

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 进入您的LinearLayout或从主RelativeLayout删除所有填充

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

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删除填充。

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. 我想您正在使用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的图形编辑器,而且我通常直接编写XML代码,因为使用编辑器确实很痛苦

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 . 希望有帮助。

Don't use fill_parent as it is deprecated. 不要使用fill_parent,因为它已被弃用。 Instead to your RelativeLayout set 改为设置您的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! 亲切的问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM