简体   繁体   中英

Android: Putting a vertical divider/separator line between textviews in a horizontal linear layout nested in vertical linear layout?

For an android content view, I have a vertical linearlayout with some textviews that have some lines to divide and separated the vertical elements, this works fine and the xml is below.

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/A" />                 
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/B" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/C" />    
    </LinearLayout>    
    <View 
         android:background="#ffffff" 
         android:layout_width = "fill_parent"
         android:layout_height="1dip"/>    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/D" />
    <View  
         android:background="#ffffff" 
         android:layout_width = "fill_parent"
         android:layout_height="1dip" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/E" />
    </LinearLayout>

Now I want to add a vertical separator line between the horizontally placed text views in the nested textviews with strings A/B/C. When I try to do so by adding the hardcoded width View, the line spans the whole height from the parent linear layout.

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/A" />         
        <!--the vertical line separator-->
        <View  
     android:background="#ffffff" 
     android:layout_width = "1dip"
     android:layout_height="fill_parent" />         
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/B" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/C" />    
    </LinearLayout>    
    <View 
         android:background="#ffffff" 
         android:layout_width = "fill_parent"
         android:layout_height="1dip"/>    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/D" />
    <View  
         android:background="#ffffff" 
         android:layout_width = "fill_parent"
         android:layout_height="1dip" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/E" />
    </LinearLayout>

For this vertical separator view I have tried to use android:layout_height="wrap_content"/> instead but the same result is presented.

Is there a way to have a vertical separator here where the height is preserved with the introduction of a vertical line to separate textviews? Or must I choose a different layout?

Another option may be to include android:layout_margin (or separate layout_marginTop and layout_marginBottom ) to create a space between the top and bottom of the drawn vertical line and the respective horizontal edges of the LinearLayout.

Other than that, I might try a RelativeLayout, and have your vertical line view adjusted to align its top and bottom to one of the adjacent TextViews.

您必须为LinearLayoutVertical Seperator提供修复高度

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