在安卓中,使用java给LinearLayout添加边框,怎么实现?
时间: 2023-05-18 11:03:30 浏览: 59
您可以使用以下代码为LinearLayout添加边框:
```
LinearLayout linearLayout = findViewById(R.id.linearLayout);
linearLayout.setBackgroundResource(R.drawable.border);
```
其中,R.drawable.border是一个XML文件,用于定义边框的样式,例如:
```
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dp" android:color="#000000" />
<solid android:color="#FFFFFF" />
</shape>
```
这个XML文件定义了一个1dp宽度、黑色颜色的边框,以及白色的背景颜色。您可以根据需要修改这个XML文件来定义不同的边框样式。
相关问题
LinearLayout添加边框
可以使用 Shape Drawable 来为 LinearLayout 添加边框,具体实现可以参考以下代码:
1. 在 res/drawable 目录下创建一个 xml 文件,比如 border.xml,代码如下:
```
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />
<stroke
android:width="1dp"
android:color="#000000" />
</shape>
```
2. 在 LinearLayout 的布局文件中,将 android:background 属性设置为 border.xml,代码如下:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border">
<!-- 子 View -->
</LinearLayout>
```
这样就可以为 LinearLayout 添加边框了。
在android12中,在LinearLayout中有一个TextView,怎么给TextView添加边框
您可以通过在TextView的XML布局文件中设置android:background属性来添加边框。例如:
```xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:background="#FF0000"
android:padding="10dp" />
```
这将在TextView周围添加一个红色边框,并设置文本的内边距。您可以调整background属性的值来更改边框的样式和颜色。