Kon*_*rov 1214
您可以将形状可绘制(矩形)设置为视图的背景.
<TextView android:text="Some text" android:background="@drawable/back"/>
Run Code Online (Sandbox Code Playgroud)
和矩形drawable back.xml(放入res/drawable文件夹):
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@android:color/white" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
您可以使用@android:color/transparent纯色来获得透明背景.您还可以使用填充来将文本与边框分开.有关详细信息,请参阅:http://developer.android.com/guide/topics/resources/drawable-resource.html
Sur*_*gch 171
让我总结一些不同的(非程序化)方法.
将以下内容保存为drawable文件夹中的XML文件(例如,my_border.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- View background color -->
<solid
android:color="@color/background_color" >
</solid>
<!-- View border color and width -->
<stroke
android:width="1dp"
android:color="@color/border_color" >
</stroke>
<!-- The radius makes the corners rounded -->
<corners
android:radius="2dp" >
</corners>
</shape>
Run Code Online (Sandbox Code Playgroud)
然后将其设置为TextView的背景:
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_border" />
Run Code Online (Sandbox Code Playgroud)
更多帮助:
9补丁是可伸缩的背景图像.如果您使用边框制作图像,则会为TextView提供边框.您需要做的就是制作图像,然后在TextView中将其设置为背景.
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_ninepatch_image" />
Run Code Online (Sandbox Code Playgroud)
以下是一些链接,将显示如何制作9补丁图像:
使用图层列表
您可以使用图层列表将两个矩形堆叠在一起.通过使第二个矩形比第一个矩形略小,您可以创建边框效果.第一个(下部)矩形是边框颜色,第二个矩形是背景颜色.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Lower rectangle (border color) -->
<item>
<shape android:shape="rectangle">
<solid android:color="@color/border_color" />
</shape>
</item>
<!-- Upper rectangle (background color) -->
<item android:top="2dp">
<shape android:shape="rectangle">
<solid android:color="@color/background_color" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
设置android:top="2dp"顶部偏移(使其变小)2dp.这允许第一个(下部)矩形显示,给出边框效果.您可以像shape上面的drawable 一样将它应用于TextView背景.
以下是有关图层列表的更多链接:
使用9补丁
您可以使用单个边框制作9补丁图像.其他一切与上面讨论的相同.
使用视图
这是一种技巧,但如果您需要在两个视图之间添加分隔符或者将边框添加到单个TextView,它会很有效.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- This adds a border between the TextViews -->
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@android:color/black" />
<TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这里有一些更多的链接:
小智 48
简单的方法是为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>
Run Code Online (Sandbox Code Playgroud)
对于其他方向边框,请调整分隔符视图的位置.
Boj*_*vic 31
我通过扩展textview并手动绘制边框解决了这个问题.我甚至添加了这样你可以选择边框是虚线还是虚线.
public class BorderedTextView extends TextView {
private Paint paint = new Paint();
public static final int BORDER_TOP = 0x00000001;
public static final int BORDER_RIGHT = 0x00000002;
public static final int BORDER_BOTTOM = 0x00000004;
public static final int BORDER_LEFT = 0x00000008;
private Border[] borders;
public BorderedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public BorderedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BorderedTextView(Context context) {
super(context);
init();
}
private void init(){
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(4);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(borders == null) return;
for(Border border : borders){
paint.setColor(border.getColor());
paint.setStrokeWidth(border.getWidth());
if(border.getStyle() == BORDER_TOP){
canvas.drawLine(0, 0, getWidth(), 0, paint);
} else
if(border.getStyle() == BORDER_RIGHT){
canvas.drawLine(getWidth(), 0, getWidth(), getHeight(), paint);
} else
if(border.getStyle() == BORDER_BOTTOM){
canvas.drawLine(0, getHeight(), getWidth(), getHeight(), paint);
} else
if(border.getStyle() == BORDER_LEFT){
canvas.drawLine(0, 0, 0, getHeight(), paint);
}
}
}
public Border[] getBorders() {
return borders;
}
public void setBorders(Border[] borders) {
this.borders = borders;
}
}
Run Code Online (Sandbox Code Playgroud)
边境班:
public class Border {
private int orientation;
private int width;
private int color = Color.BLACK;
private int style;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getStyle() {
return style;
}
public void setStyle(int style) {
this.style = style;
}
public int getOrientation() {
return orientation;
}
public void setOrientation(int orientation) {
this.orientation = orientation;
}
public Border(int Style) {
this.style = Style;
}
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于某人:)
Gab*_*tti 16
通过材料组件库,您可以使用MaterialShapeDrawable.
<TextView
android:id="@+id/textview"
.../>
Run Code Online (Sandbox Code Playgroud)
然后您可以以编程方式应用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);
Run Code Online (Sandbox Code Playgroud)
小智 14
我只是在看一个类似的答案 - 它可以通过一个笔画和以下覆盖来完成:
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Paint strokePaint = new Paint();
strokePaint.setARGB(255, 0, 0, 0);
strokePaint.setTextAlign(Paint.Align.CENTER);
strokePaint.setTextSize(16);
strokePaint.setTypeface(Typeface.DEFAULT_BOLD);
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(2);
Paint textPaint = new Paint();
textPaint.setARGB(255, 255, 255, 255);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(16);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
canvas.drawText("Some Text", 100, 100, strokePaint);
canvas.drawText("Some Text", 100, 100, textPaint);
super.draw(canvas, mapView, shadow);
}
Run Code Online (Sandbox Code Playgroud)
Mau*_*oki 13
您可以通过两种方法设置边框.一个是可绘制的,第二个是程序化的.
<shape>
<solid android:color="@color/txt_white"/>
<stroke android:width="1dip" android:color="@color/border_gray"/>
<corners android:bottomLeftRadius="10dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="10dp"
android:topRightRadius="0dp"/>
<padding android:bottom="0dip"
android:left="0dip"
android:right="0dip"
android:top="0dip"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
编程
public static GradientDrawable backgroundWithoutBorder(int color) {
GradientDrawable gdDefault = new GradientDrawable();
gdDefault.setColor(color);
gdDefault.setCornerRadii(new float[] { radius, radius, 0, 0, 0, 0,
radius, radius });
return gdDefault;
}
Run Code Online (Sandbox Code Playgroud)
Pro*_*ean 13
我找到的最简单的解决方案(实际上有效):
<TextView
...
android:background="@android:drawable/editbox_background" />
Run Code Online (Sandbox Code Playgroud)
Mar*_*hat 12
您可以将可绘制的形状(带角的矩形)设置为视图的背景。
<TextView android:background="@drawable/frame"/>
Run Code Online (Sandbox Code Playgroud)
和矩形可绘制frame.xml(放入res/drawable文件夹中):
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@android:color/white" />
<stroke android:width="1dip"
android:color="#3d4caf"/>
<corners android:radius="50dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
Nic*_*ick 11
我找到了一种更好的方法来在TextView周围放置边框.
使用九个补丁图像作为背景.这很简单,SDK附带了一个制作9补丁图像的工具,它绝对不需要编码.
该链接是http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch.
小智 11
您可以在代码中添加以下内容:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
Bah*_*any 10
检查下面的链接以制作圆角 http://androidcookbook.com/Recipe.seam?recipeId=2318
Android项目中res下的drawable文件夹不限于位图(PNG或JPG文件),但它也可以保存XML文件中定义的形状.
然后可以在项目中重用这些形状.形状可用于在布局周围放置边框.此示例显示了带有弯角的矩形边框.在drawable文件夹中创建一个名为customborder.xml的新文件(在Eclipse中使用File菜单并选择New then File,在文件名中选择drawable文件夹,然后单击Finish).
输入定义边框形状的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"/>
<solid android:color="#CCCCCC"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
该属性android:shape设置为矩形(形状文件也支持椭圆,直线和圆环).Rectangle是默认值,因此如果它是一个定义的矩形,则可以省略该属性.有关形状文件的详细信息,请参阅http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape上有关形状的Android文档.
元素角将矩形角设置为圆角.可以在每个角上设置不同的半径(请参阅Android参考).
填充属性用于移动应用了形状的视图的内容,以防止内容与边框重叠.
此处的边框颜色设置为浅灰色(CCCCCC十六进制RGB值).
形状也支持渐变,但这里没有使用.再次,请参阅Android资源以查看如何定义渐变.使用将形状应用于套管android:background="@drawable/customborder".
在布局中,可以正常添加其他视图.在此示例中,添加了单个TextView,文本为白色(FFFFFF十六进制RGB).背景设置为蓝色,加上一些透明度以降低亮度(A00000FF十六进制alpha RGB值).最后,布局通过将其放入具有少量填充的另一布局来偏离屏幕边缘.因此,完整的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/customborder">
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Text View"
android:textSize="20dp"
android:textColor="#FFFFFF"
android:gravity="center_horizontal"
android:background="#A00000FF" />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我有办法非常简单地完成它,我想分享它.
当我想对mi TextViews进行平方时,我只是将它们放在LinearLayout中.我设置了LinearLayout的背景颜色,并为TextView添加了一个边距.结果就像你对TextView进行平方.
小智 5
You can create custom background for your text view. Steps
<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>
Run Code Online (Sandbox Code Playgroud)
For your text view where you want to use it as backgroud,
android:background="@drawable/your_fileName"
| 归档时间: |
|
| 查看次数: |
679817 次 |
| 最近记录: |