分享

自定义Button形状(圆形、椭圆)

 muyable 2014-05-02

这里介绍2种改变Button形状的方法,一个是直接通过设置一张圆形背景图片,

另一种方法是通过引用自定义的shape 文件。具体参见如下:

 

 

 

--------main_menu_btnshape.xml              自定义形状xml文件

 

<?xml version="1.0" encoding="utf-8"?>

<shape

  xmlns:android="http://schemas./apk/res/android"

  android:shape="oval">

    <!-- 填充的颜色 -->

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

     <!-- 设置按钮的四个角为弧形 -->

     <!-- android:radius 弧形的半径 -->

     <corners android:radius="360dip"/>

    

     <!-- padding: Button 里面的文字与Button边界的间隔 -->

     <padding

        android:left="10dp"

        android:top="10dp"

        android:right="10dp"

        android:bottom="10dp"

     />

</shape>

 

 

-----Main    layout文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas./apk/res/android"

    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:text="@string/soft_info"

/>

 

<!—直接设置背景 -->

<Button

    android:id="@+id/roundBtn1"

    android:background="@drawable/btn_oval"

    android:layout_width="50dip"

    android:layout_height="50dip"

    />   

 

 

<!—  调用shape自定义xml文件 -->

<Button

    android:id="@+id/roundBtn"

    android:text="椭圆按钮"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:background="@drawable/main_menu_btnshape"

    />   

   

</LinearLayout>

 

 

----acitivity文件

public class MyLifeActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

}

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多