简体   繁体   中英

Android Layout: How to add bottom and right border into linear layout together?

I have the following linear layout, which is used as table cell. I would like to add to selected templates right and bottom border. But now I'm able to add only border on right.

How can i add border to selected sides at once?

Many thanks for any advice.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="60dp"
    android:layout_height="80dp"
    android:gravity="right"
    android:background="@color/white"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_horizontal|center"
        android:gravity="center_horizontal|center"
        android:layout_marginRight="10dp"
        android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/variable_type_first_column"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/heart"
        android:layout_gravity="center_horizontal|center_vertical"
        android:gravity="center"/>

    <TextView
        android:id="@android:id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Weight"
        android:layout_below="@id/variable_type_first_column"
        android:layout_gravity="center_horizontal|bottom"
        android:gravity="center_horizontal|bottom"
        android:textColor="@color/app_blue"
        android:textAlignment="center"
        android:textSize="12sp"/>

    </LinearLayout>
    <View
        android:layout_width="1.5dip"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:foregroundGravity="right"
        android:background="@drawable/separator_table_header_first" />

</LinearLayout>

Create drawable file inside your drawable folder.

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="5dp"
    android:insetLeft="-7dp"
    android:insetRight="5dp"
    android:insetTop="-7dp">
    <shape android:shape="rectangle">
        <stroke
            android:width="5dp"
            android:color="#ff0000" />
        <solid android:color="#00000000" />
    </shape>
</inset>

and set it as background to linear layout.

The simplest way would be to wrap the LinearLayout in a FrameLayout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="60dp"
    android:layout_height="80dp"
    android:gravity="right"
    android:background="@color/white"
    >

    <LinearLayout
        ...
        Your layout without the divider Views
        ...
    </LinearLayout>

    <View
        android:layout_width="1.5dip"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:foregroundGravity="right"
        android:background="@drawable/separator_table_header_first" 
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="1.5dp"
        android:layout_gravity="bottom"
        android:foregroundGravity="bottom"
        android:background="@drawable/separator_table_header_first" 
        />

</FrameLayout>

You could design a flatter XML utilizing RelativeLayouts though which might be more efficient.

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