Tôi có một bố cục lớn và một bố cục nhỏ hơn bên trong nó.Tôi làm cách nào để tạo đường viền xung quanh LinearLayout của Android?
Làm cách nào để tạo đường viền đường viền quanh bố cục nhỏ?
Tôi có một bố cục lớn và một bố cục nhỏ hơn bên trong nó.Tôi làm cách nào để tạo đường viền xung quanh LinearLayout của Android?
Làm cách nào để tạo đường viền đường viền quanh bố cục nhỏ?
Chắc chắn. Bạn có thể thêm đường viền vào bất kỳ bố cục nào bạn muốn. Về cơ bản, bạn cần tạo một drawable tùy chỉnh và thêm nó làm nền cho layout của bạn. Ví dụ:
Tạo một tập tin gọi customborder.xml
trong thư mục drawable của bạn:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
<stroke android:width="1dp" android:color="#CCCCCC"/>
</shape>
Bây giờ áp dụng nó như là một nền tảng để bố trí nhỏ hơn của bạn:
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/customborder">
Điều đó sẽ làm các trick.
Xem thêm:
Bí quyết ở đây thực sự là
Cả hai '
Tạo XML gọi border.xml trong thư mục drawable như sau:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item android:left="5dp" android:right="5dp" android:top="5dp" >
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
sau đó thêm này để bố trí tuyến tính như backgound như sau:
android:background="@drawable/border"
Hãy thử điều này:
Ví dụ, chúng ta hãy xác định res/drawable/my_custom_background.xml như:
(tạo bố trí này trong thư mục drawable của bạn) layout_border.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:width="2dp" android:height="2dp"
android:color="#FF0000" />
<solid android:color="#000000" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
<corners android:radius="1dp" android:bottomRightRadius="5dp"
android:bottomLeftRadius="0dp" android:topLeftRadius="5dp"
android:topRightRadius="0dp" />
</shape>
</item>
</layer-list>
main.xml
<LinearLayout
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/layout_border" />
</LinearLayout>
Crea te một tập tin một xml trong thư mục drawable
<stroke
android:width="2dp"
android:color="#B40404" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<corners android:radius="4dp" />
Bây giờ gọi xml này để nền bố trí nhỏ của bạn
android: background = "@ drawable/yourxml"
Yếu tố đột quỵ không được phép ở đây. – hdavidzhu
Giải pháp này sẽ chỉ thêm đường viền, phần thân của LinearLayout sẽ trong suốt.
Thứ nhất, Tạo drawable biên giới này trong thư mục drawable, border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="2dp" android:color="#ec0606"/>
<corners android:radius="10dp"/>
</shape>
Sau đó, trong LinearLayout Xem của bạn, thêm biên giới.xml làm hình nền như thế này
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/border">
bạn có thể làm điều đó một cách thực dụng cũng
GradientDrawable gradientDrawable=new GradientDrawable();
gradientDrawable.setStroke(4,getResources().getColor(R.color.line_Input));
Sau đó thiết lập nền tảng của bố cục như:
LinearLayout layout = (LinearLayout) findViewById(R.id.ayout); layout .setBackground(gradientDrawable);
Tôi sẽ thêm tài liệu Android liên kết đến khác câu trả lời.
https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
Nó mô tả tất cả các thuộc tính của Shape drawable và stroke
trong số đó để thiết lập biên giới.
Ví dụ:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="#F00"/>
<solid android:color="#0000"/>
</shape>
Red biên giới với nền trong suốt.
Bạn đã thử http://stackoverflow.com/questions/3496269/how-to-put-a-border-around-an-android-textview? – sandrstar