简体   繁体   中英

Can't create a view in layout

i've got a ListView with a custom adapter so i can customize the single row. I would create a view in the layout on the left that has as height all row height, and as width something like 5dp. So it will be like a little "strip" on the left of each row in the listview. I tried in this way

<?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:layout_margin="5dp"
    android:background="@layout/cardwhite"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/first"
        android:layout_toRightOf="@+id/viewlist"
        android:layout_marginLeft="5dp"
        android:textSize="15dp"
        android:fontFamily="sans-serif-light"
        android:text="Partenza "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <View 
        android:id="@id/viewlist"
        android:gravity="left"
        android:layout_width="5dp"
        android:background="#52BD4F"
        android:layout_margin="5dp"
        android:layout_height="match_parent"
    />
    <TextView
        android:id="@+id/second"
        android:layout_toRightOf="@id/viewlist"
        android:layout_below="@id/first"
        android:textSize="15dp"
        android:layout_marginLeft="5dp"
        android:fontFamily="sans-serif-light"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


</RelativeLayout>

But using android:layout_height="match_parent" or android:layout_height="wrap_content" not works and the strip doesn't appear. If i write android:layout_height="20dp" it goes but of course it's not "dynamic". I don't understand why it desappears.. Any ideas?

Please modify the layout accordingly. I have already done this one, so I'm just pasting it. You just need to make use of android:layout_alignBottom and android:layout_alignTop with proper id inside your View . Here is the screen shot !

截图

<?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"
    android:background="#e5e4e2" >

    <RelativeLayout
        android:id="@+id/relative_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/btn_normal" >

        <TextView
            android:layout_width="5dp"
            android:layout_height="match_parent"
            android:layout_alignBottom="@+id/tv_type"
            android:layout_alignTop="@+id/tv_dayName"
            android:background="#00ff00" />

        <TextView
            android:id="@+id/tv_dayName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_gravity="center_vertical"
            android:padding="5dp"
            android:layout_marginLeft="5dp"
            android:text="Monday"
            android:textColor="#000000"
            android:textSize="14sp" >
        </TextView>

        <TextView
            android:id="@+id/tv_dateNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_gravity="center_vertical"
            android:padding="5dp"
            android:text="23"
            android:textColor="#000000"
            android:textSize="14sp" >
        </TextView>

        <TextView
            android:id="@+id/tv_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_dayName"
            android:layout_gravity="center_vertical"
            android:padding="5dp"
            android:layout_marginLeft="5dp"
            android:text="Mark Attendance"
            android:textColor="#000000"
            android:textSize="12sp" >
        </TextView>

        <TextView
            android:id="@+id/tv_monthName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/tv_dateNumber"
            android:layout_gravity="center_vertical"
            android:padding="5dp"
            android:text="Apr-14"
            android:textColor="#000000"
            android:textSize="12sp" >
        </TextView>
    </RelativeLayout>

    <!--
          <TextView android:id="@+id/tv_line"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/text_line"
        android:layout_below="@+id/relative_row"/>
    -->

</RelativeLayout>

Here is a small trick. Instead of giving height to the view use a TextView and give Height as MatchParant and set TextSize according to your need and set Background color to your TextView. Hope this helps you!

<TextView
   android:id="@+id/col_status"
   android:layout_width="5dp"
   android:layout_height="match_parent"
   android:layout_marginBottom="3dp"
   android:layout_marginLeft="3dp"
   android:layout_marginTop="3dp"
   android:background="#FFFFFF"
   android:rotation="180"
   android:textColor="@color/main_list_text"
   android:textSize="12sp" />
<?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:layout_margin="5dp"
    android:background="@layout/cardwhite"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/first"
        android:layout_toRightOf="@+id/viewlist"
        android:layout_marginLeft="5dp"
        android:textSize="15dp"
        android:fontFamily="sans-serif-light"
        android:text="Partenza "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <View 
        android:id="@id/viewlist"
        android:gravity="left"
        android:layout_alignTop="@+id/first"
        android:layout_alignBottom="@+id/second"
        android:layout_width="5dp"
        android:background="#52BD4F"
        android:layout_margin="5dp"
        android:layout_height="match_parent"
    />
    <TextView
        android:id="@+id/second"
        android:layout_toRightOf="@id/viewlist"
        android:layout_below="@id/first"
        android:textSize="15dp"
        android:layout_marginLeft="5dp"
        android:fontFamily="sans-serif-light"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


</RelativeLayout>

Dont use relative layout just linearLayout

sample:

Beware that I dont have your cardwhite image so it wont be look like that one that you posted but just put the image in the linear layout background and it will look like the same as the one you posted.

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@layout/cardwhite"
    android:orientation="horizontal" >

    <View
        android:id="@id/viewlist"
        android:layout_width="5dp"
        android:layout_height="match_parent"
        android:background="#52BD4F" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/first"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:fontFamily="sans-serif-light"
            android:text="Customer 1"
            android:textSize="15dp" />

        <TextView
            android:id="@+id/second"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:fontFamily="sans-serif-light"
            android:text="Meeting"
            android:textSize="20dp" />
    </LinearLayout>

</LinearLayout>

result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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