解决包括布局冲突

时间:2014-08-25 10:05:37

标签: android xml

所以我有3个布局如下:

first.xml

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

    <ImageView 
        android:id="@+id/reward_icon"
        android:layout_marginLeft="15dp"
        android:layout_height="55dp"
        android:layout_width="55dp"
        android:src="@drawable/gift"/>

    <TextView
        android:id="@+id/outlet_name"
        android:layout_toRightOf="@id/reward_icon"
        android:layout_marginRight="45dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "15dip"
        android:hint="outlet"
        />
    <TextView 
        android:id="@+id/distance"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:hint="0.0km"/>

    <TextView
        android:id="@+id/reward"
        android:layout_toRightOf="@id/reward_icon"
        android:layout_toLeftOf="@id/distance"
        android:layout_below="@id/outlet_name"
        android:layout_marginRight="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "15dip"
        android:hint="Reward"
        />
    <TextView
        android:id="@+id/locality"
        android:layout_toRightOf="@id/reward_icon"
        android:layout_toLeftOf="@id/distance"
        android:layout_below="@id/reward"
        android:layout_marginRight="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "15dip"
        android:hint="Locality"
        />
    </RelativeLayout>

second.xml

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

    <ImageView 
        android:id="@+id/logo"
        android:layout_marginLeft="15dp"
        android:layout_height="55dp"
        android:layout_width="55dp"
        android:src="@drawable/fav_icon"/>

    <TextView
        android:id="@+id/outlet_name"
        android:layout_toRightOf="@id/logo"
        android:layout_marginRight="45dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "15dip"
        android:hint="outlet"
        />
    <TextView 
        android:id="@+id/distance"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:hint="0.0km"/>

    <TextView
        android:id="@+id/reward"
        android:layout_toRightOf="@id/logo"
        android:layout_toLeftOf="@id/distance"
        android:layout_below="@id/outlet_name"
        android:layout_marginRight="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "15dip"
        android:hint="Reward"
        />
    <TextView
        android:id="@+id/locality"
        android:layout_toRightOf="@id/logo"
        android:layout_toLeftOf="@id/distance"
        android:layout_below="@id/reward"
        android:layout_marginRight="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "15dip"
        android:hint="Locality"
        />
    </RelativeLayout>

third.xml

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

    <ImageView 
        android:id="@+id/fav_icon"
        android:layout_marginLeft="15dp"
        android:layout_height="55dp"
        android:layout_width="55dp"
        android:src="@drawable/heart"/>

    <TextView
        android:id="@+id/outlet_name"
        android:layout_toRightOf="@id/fav_icon"
        android:layout_marginRight="45dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "15dip"
        android:hint="outlet"
        />
    <TextView 
        android:id="@+id/distance"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:hint="0.0km"/>

    <TextView
        android:id="@+id/reward"
        android:layout_toRightOf="@id/fav_icon"
        android:layout_toLeftOf="@id/distance"
        android:layout_below="@id/outlet_name"
        android:layout_marginRight="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "15dip"
        android:hint="Reward"
        />
    <TextView
        android:id="@+id/locality"
        android:layout_toRightOf="@id/fav_icon"
        android:layout_toLeftOf="@id/distance"
        android:layout_below="@id/reward"
        android:layout_marginRight="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "15dip"
        android:hint="Locality"
        />
    </RelativeLayout>

现在我按如下方式创建了一个布局all.xml:

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

    <include layout="@layout/first" />

    <include layout="@layout/second" />

    <include layout="@layout/third" />

</LinearLayout>

我的问题是当我使用all.xml时,image_view显示所有数据的reward_icon是否应该显示第一组数据的reward_icon,第二类数据的logo和第三类数据的fav_icon。我使用适配器如下:

 String[] values = {"outlet_name","reward","locality","distance"};
                    int[] ids = {R.id.outlet_name,R.id.reward,R.id.locality,R.id.distance};
                    adapter = new SimpleAdapter(getActivity(), oslist, 
                                R.layout.all, values,ids);
                    list.setAdapter(adapter);
                      ((SimpleAdapter) adapter).notifyDataSetChanged();

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

1.Add this properties android:orientation="vertical" to your LinearLayout in all.xml
2.Change this properties android:layout_height="match_parent" to android:layout_height="wrap_content" RelativeLayout in first.xml,second.xml and third.xml.