大きなレイアウトが 1 つと、その中に小さなレイアウトが 1 つあります。
小さなレイアウトの周りに境界線を作成するにはどうすればよいですか?
大きなレイアウトが 1 つと、その中に小さなレイアウトが 1 つあります。
小さなレイアウトの周りに境界線を作成するにはどうすればよいですか?
もちろん。必要なレイアウトに境界線を追加できます。基本的に、カスタムドローアブルを作成し、それを背景としてレイアウトに追加する必要があります。例:
customborder.xml
ドローアブルフォルダにというファイルを作成します。
<?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>
次に、それを背景として小さいレイアウトに適用します。
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/customborder">
それでうまくいくはずです。
以下も参照してください。
以下のように、drawable フォルダーに border.xml という XML を作成します。
<?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>
次に、これを背景として線形レイアウトに追加します。
android:background="@drawable/border"
これを試して:
たとえば、 res/drawable/my_custom_background.xml を次のように定義しましょう。
(ドローアブル フォルダーにこのレイアウトを作成します) 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>
drawable フォルダーに 1 つの xml ファイルを作成する
<stroke
android:width="2dp"
android:color="#B40404" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<corners android:radius="4dp" />
このxmlを小さなレイアウトの背景に呼び出します
android:background="@drawable/yourxml"
あなたは実用的にもそれを行うことができます
GradientDrawable gradientDrawable=new GradientDrawable();
gradientDrawable.setStroke(4,getResources().getColor(R.color.line_Input));
次に、レイアウトの背景を次のように設定します。
LinearLayout layout = (LinearLayout ) findViewById(R.id.ayout); layout .setBackground(gradientDrawable);
Android ドキュメントのリンクを他の回答に追加します。
https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
Shape Drawable のすべての属性を記述し、stroke
その中で境界線を設定します。
例:
<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>
背景が透明な赤い枠。