6

是否可以在 main.xml 中的 Android 按钮中指定边框?[ps 没有“包含笔划标记的单独 xml 文件”,但在我定义按钮的原始文件中,也没有“通过编程动态”解决方案和“图像”解决方案]

  <Button
    android:background="#FFFFFF"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:text="Player 3"
    android:layout_x="0px"
    android:layout_y="0px"
    android:id="@+id/p3"
    android:layout_weight="1" 

/>  

在这里,我正在动态更改背景,但问题是,对于 2 个按钮,没有边框。

4

2 回答 2

35

尝试使用形状

my_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:radius="0.1dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />

    <solid android:color="#FFFFFF" />

    <stroke
        android:width="1dp"
        android:color="#E8E6E7" />

</shape>

和按钮

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/my_shape"
    android:text="Button" />

截屏

在此处输入图像描述

我希望这能帮到您。

于 2013-07-10T07:59:12.307 回答
7

这不是推荐的方法,因为它会导致过度绘制并添加不必要的视图,Gunaseelan 有正确的方法


没有边界作为属性的概念。公认的方法是使用单独的可绘制对象作为背景View(使用stroke您提到的和@gunaseelan 所写的)。

另一种(不推荐)方法是将您的封装Button在另一个中,View例如 a LinearLayout,将背景颜色设置为封装上所需的边框颜色,View并在外部进行填充View

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="#342334"
    android:padding="5dp"
    >
    <Button
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="whatwhaaat"
        />
</LinearLayout>

padding 的值将指示边框的粗细。不建议使用此方法,因为您最终会View在布局中添加一个额外的层,以及另一层过度绘制(它会在顶部绘制LinearLayout然后)。Button

于 2013-07-10T08:24:36.383 回答