简体   繁体   中英

how to design layout button to be half one color, half other

I need to design button for android app to be rectangle, and to be half one color, half other. In XML of course.

I have tried this:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
    android:left="7dp"
    android:top="15dp"
    android:right="7dp"
    android:bottom="15dp" />
<gradient
    android:startColor="#a241cb"
    android:endColor="#a241cb"
    android:type="linear"
    android:angle="90"/>
<stroke>
    android:width="1dip"
    android:color="#a241cb" />
<corners android:radius="0dp" />
</shape>

Any help is appreciated

Try this:

frist create background.xml in drwable folder

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#4A225E"
        android:endColor="#4C4064"
        android:centerColor="#B4AFC5"/>
</shape>

and then set button backgrond its work for me

you can achieve this by using gradients as background.
use this create a new XML file under res/drawable folder and name it as button_bg.xml . button_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#D5DDE0"
      android:centerColor="#e7e7e8"
      android:endColor="#CFCFCF"
      android:angle="270" />
</shape>

now use it for your Button like this:

android:background="@drawable/button_bg"

Hope it helps. :)

Using a 9 patch like this (save it in your drawable folder as bw_bg.9.png ):

在此处输入图片说明

You can get a result like this:

在此处输入图片说明

Example layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f00f"
    >
    <Button
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:layout_centerInParent="true"
        android:background="@drawable/bw_bg"
    />
</RelativeLayout>

Don't mind the size (it's running on a Froyo 2.8 inch screen).
Also don't mind the notification (it's from another running app).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM