如何在Android中给出两个线性布局之间的空间

时间:2014-01-16 08:52:16

标签: android android-layout

我正在使用以下代码生成页面。我能够在线性布局之间留出空间,这个空间正好在另一个之下。但是当我试图在两个彼此相邻的线性布局之间做它不适合我。对不起,我可能没有正确解释,但当你看到我的代码时,你将能够理解我的问题。

顶部和底部有空间,但顶部有两个线性布局,但我无法在它们之间留出空间。

这就是我想要的:

enter image description here

我在这里粘贴我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >


<ImageView
    android:id="@+id/userImage"
    android:layout_width="150dp"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/products_category" />

  <TextView
    android:id="@+id/userName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/userImage"
    android:layout_centerHorizontal="true"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/black" />


    <LinearLayout
        android:layout_below="@+id/userName"
        android:id="@+id/firstLinear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:layout_centerInParent="true"
        android:layout_marginLeft="80dp"
        android:paddingTop="25dp">

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="125dp"
            android:background="@color/black"
            android:orientation="vertical"
            android:layout_gravity="center">

            <TextView
                android:id="@+id/userLocationText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/location_text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white"
                android:layout_gravity="center" />

            <ImageView
                android:id="@+id/userLocationImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/location_icon"
                android:contentDescription="@string/products_category"
                android:layout_gravity="center" />

            <TextView
                android:id="@+id/userLocation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white"
                android:layout_gravity="center" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="125dp"
            android:background="@color/black"
            android:orientation="vertical">

            <TextView
                android:id="@+id/userZoneText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/zone_text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white"
                android:layout_gravity="center" />

            <ImageView
                android:id="@+id/userZoneImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/zone_icon"
                android:contentDescription="@string/products_category"
                android:layout_gravity="center" />

            <TextView
                android:id="@+id/userZone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white"
                android:layout_gravity="center" />
        </LinearLayout>

    </LinearLayout>
  <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:layout_below="@+id/firstLinear"
        android:padding="5dp"
        android:layout_centerInParent="true">
        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="125dp"
            android:background="@color/black"
            android:orientation="vertical" 
            android:paddingRight="5dp" >

            <TextView
                android:id="@+id/userPointsText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/points_text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white"
                android:layout_gravity="center" />

            <ImageView
                android:id="@+id/userPointsImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/points_icon"
                android:contentDescription="@string/products_category"
                android:layout_gravity="center" />

            <TextView
                android:id="@+id/userPoints"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white"
                android:layout_gravity="center" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="125dp"
            android:background="@color/black"
            android:orientation="vertical">

            <TextView
                android:id="@+id/userLevelText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/level_text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white"
                android:layout_gravity="center" />

            <ImageView
                android:id="@+id/userLevelImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/level_icon"
                android:contentDescription="@string/products_category"
                android:layout_gravity="center" />

            <TextView
                android:id="@+id/userLevel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white"
                android:layout_gravity="center" />
        </LinearLayout>
  </LinearLayout>

非常感谢...

3 个答案:

答案 0 :(得分:1)

试试这个

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/userImage"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/products_category" />

    <TextView
        android:id="@+id/userName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userImage"
        android:layout_centerHorizontal="true"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/black" />

    <LinearLayout
        android:id="@+id/firstLinear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userName"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:layout_marginLeft="80dp"
        android:orientation="horizontal"
        android:paddingTop="25dp" >

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="125dp"
            android:layout_gravity="center"
            android:background="@color/black"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/userLocationText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:singleLine="true"
                android:text="@string/location_text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white" />

            <ImageView
                android:id="@+id/userLocationImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/location_icon"
                android:contentDescription="@string/products_category" />

            <TextView
                android:id="@+id/userLocation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="125dp"
            android:layout_marginLeft="20dp"
            android:background="@color/black"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/userZoneText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:singleLine="true"
                android:text="@string/zone_text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white" />

            <ImageView
                android:id="@+id/userZoneImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/zone_icon"
                android:contentDescription="@string/products_category" />

            <TextView
                android:id="@+id/userZone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/firstLinear"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:padding="5dp" >

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="125dp"
            android:background="@color/black"
            android:orientation="vertical"
            android:paddingRight="5dp" >

            <TextView
                android:id="@+id/userPointsText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:singleLine="true"
                android:text="@string/points_text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white" />

            <ImageView
                android:id="@+id/userPointsImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/points_icon"
                android:contentDescription="@string/products_category" />

            <TextView
                android:id="@+id/userPoints"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="100dp"
            android:layout_height="125dp"
            android:layout_marginLeft="20dp"
            android:background="@color/black"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/userLevelText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:singleLine="true"
                android:text="@string/level_text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white" />

            <ImageView
                android:id="@+id/userLevelImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/level_icon"
                android:contentDescription="@string/products_category" />

            <TextView
                android:id="@+id/userLevel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/white" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

答案 1 :(得分:0)

有两种方法可以解决您的问题

将右边距赋予“firstLinear”linearLayout的第一个linearLayout

 <LinearLayout
        android:layout_width="100dp"
        android:layout_height="125dp"
        android:layout_gravity="center"
       **android:layout_marginRight="30dp"** 
        android:background="@color/black"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/userLocationText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:singleLine="true"
            android:text="@string/location_text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/white" />

        <ImageView
            android:id="@+id/userLocationImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/location_icon"
            android:contentDescription="@string/products_category" />

        <TextView
            android:id="@+id/userLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/white" />
    </LinearLayout>

或将左边距赋予“firstLinear”中的第二个线性布局。

<LinearLayout
        android:layout_width="100dp"
        android:layout_height="125dp"
        android:background="@color/black"
        **android:layout_marginLeft="30dp"**
        android:orientation="vertical">

        <TextView
            android:id="@+id/userZoneText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="@string/zone_text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/white"
            android:layout_gravity="center" />

        <ImageView
            android:id="@+id/userZoneImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/zone_icon"
            android:contentDescription="@string/products_category"
            android:layout_gravity="center" />

        <TextView
            android:id="@+id/userZone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/white"
            android:layout_gravity="center" />
    </LinearLayout>

答案 2 :(得分:0)

对于您的第二个问题:使用相对布局代替图像视图,并将大图像设置为相对图像的背景,然后为相对布局中的另一个图像添加图像视图。用 -

替换ImageView
<RelativeLayout 
android:layout_width="500dp"
android:layout_height="500dp"
android:background="@drawable/largeUserImage" >

<ImageView 
    android:layout_width="100dp"
    android:layout_height="100dp"    
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"    
    android:src="@drawable/smallUserImage"/>
</RelativeLayout >