是否可以在Android TextView周围绘制边框?
当前回答
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@android:color/black" />
这段代码足够你可以放在任何地方
其他回答
在xml文本视图上设置背景,
将roundd_textview.xml文件添加到可绘制目录中。
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@android:color/white" />
<stroke android:width="2dip" android:color="#4f5g52"/>
</shape>
在textView背景中设置可绘制文件。
使用材质组件库,可以使用MaterialShapeDrawable。
<TextView
android:id="@+id/textview"
.../>
然后可以通过编程方式应用MaterialShapeDrawable:
TextView textView = findViewById(R.id.textview);
MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable();
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,android.R.color.transparent));
shapeDrawable.setStroke(1.0f, ContextCompat.getColor(this,R.color....));
ViewCompat.setBackground(textView,shapeDrawable);
您可以为文本视图创建自定义背景。步骤
转到您的项目。转到资源,右键单击可绘制。单击新建->可绘制资源文件为您的文件命名在文件中粘贴以下代码
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="@color/colorBlack" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="6dp" />
<solid android:color="#ffffffff" />
</shape>
对于要将其用作背景的文本视图,android:background=“@drawbable/your_fileName”
我找到了一种更好的方法来在TextView周围添加边框。
使用九块图像作为背景。它非常简单,SDK附带了一个制作9补丁图像的工具,而且它绝对不需要编码。
链接是http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-补丁。
简单的方法是为TextView添加视图。底部边框线示例:
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="@string/title"
android:id="@+id/title_label"
android:gravity="center_vertical"/>
<View
android:layout_width="fill_parent"
android:layout_height="0.2dp"
android:id="@+id/separator"
android:visibility="visible"
android:background="@android:color/darker_gray"/>
</LinearLayout>
对于其他方向的边框,请调整分隔符视图的位置。
推荐文章
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?
- androidviewpager与底部点
- 相同的导航抽屉在不同的活动
- 如何从视图中获得托管活动?
- 单一的TextView与多种颜色的文本
- 如何在非活动类(LocationManager)中使用getSystemService ?
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置