簡體   English   中英

視圖未顯示在RelativeLayout中

[英]View not showing in RelativeLayout

我正在嘗試使視圖,藍色條顯示在列表中,但似乎不行。 注意:稍后在創建列表視圖時設置圖像

<?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="wrap_content" 
    android:background="@color/white">
    <LinearLayout 
        android:id="@+id/list_img_ll"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">

        <ImageView 
            android:id="@+id/imgList1"
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            />

    </LinearLayout>

    <View android:id="@+id/state_open"
       android:layout_alignParentRight="true"
       android:layout_width="20dp"
       android:layout_height="match_parent"
       android:background="@color/CVBlue" /> 
</RelativeLayout>

getView()方法

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    RecordHolder holder = null;

    if (row == null) {

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new RecordHolder();
        holder.imageItem = (ImageView) row.findViewById(R.id.imgList1);
        holder.stateView = row.findViewById(R.id.state_open);

        row.setTag(holder);

    } else {

        holder = (RecordHolder) row.getTag();

    }

    Item item = data.get(position);
    holder.imageItem.setImageBitmap(item.getImage());




    return row;

}

這就是結果。 請注意,右上角似乎嘗試生成但不起作用。 另外,當我設置relativelayout的高度時,它可以工作,而且我很困惑。 不工作的圖片http://i.stack.imgur.com/xAlpg.png

這是我想要的,無需配置相對布局大小http://i.stack.imgur.com/SCRTu.png

問題是您的LinearLayout。 添加以下屬性:

android:layout_toLeftOf="@+id/state_open"

問題是您將LinearLayout的寬度設置為match_parent,這占用了整個屏幕的寬度。 不會為View留下任何空間。 通過將其設置為左側,可以防止其占用視圖的所有可用空間。

得到它了。 很奇怪。 在state_open視圖上添加:

android:layout_alignTop="@+id/list_img_ll"
android:layout_alignBottom="@+id/list_img_ll"

問題是state_open視圖不知道給出顏色值的高度...盡管視圖正在獲得適當的高度。 將其與LinearLayout對齊即可。 替代解決方案是只給View提供實際的dp大小,而不是match_parent。 即80dp。 有史以來最奇怪的事情。

嘗試這個:

<?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="wrap_content" 
    android:background="@color/white">
    <View android:id="@+id/state_open"
       android:layout_alignParentRight="true"
       android:layout_width="20dp"
       android:layout_height="match_parent"
       android:background="@color/CVBlue" />

    <LinearLayout 
        android:id="@+id/list_img_ll"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_toLeftOf="@id/state_open">

        <ImageView 
            android:id="@+id/imgList1"
            android:layout_width="wrap_content"
            android:layout_height="80dp"/>

    </LinearLayout> 
</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM