自定义按钮

来源:互联网 发布:西门子plc编程电缆 编辑:程序博客网 时间:2024/06/06 03:43
<com.awtrip.view.CustomRadioButton
android:id="@+id/sousuo_CustomRadioButton"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/lanseanniu_selector"
custom:LeftTextDrawable="@drawable/jiudianyuding_sousuo"
custom:Text="搜索"
custom:TextSize="8sp"
custom:TextColor="@color/baise" />

lanseanniu_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<shape >
<solid android:color="#8858B1E5"/>
<corners android:radius="5dp" />
</shape>
</item>
<item android:state_pressed="false">
<shape >
<solid android:color="#FF58B1E5"/>
<corners android:radius="5dp" />
</shape>
</item>

</selector>

public class CustomRadioButton extends RelativeLayout {

private String text;
private Drawable drawable, leftDrawable;
private int textColor;
private float textSize;

private TextView textView;
private ImageView imageView, leftImageView;

/**
* @param context
* @param attrs
*/
public CustomRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);

LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_custom_radiobutton, this);

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomRadioButton);
text = ta.getString(R.styleable.CustomRadioButton_Text);
textColor = ta.getColor(R.styleable.CustomRadioButton_TextColor, 0);
drawable = ta.getDrawable(R.styleable.CustomRadioButton_TextDrawable);
leftDrawable = ta.getDrawable(R.styleable.CustomRadioButton_LeftTextDrawable);
textSize = ta.getDimension(R.styleable.CustomRadioButton_TextSize, 14);

ta.recycle();

setGravity(Gravity.CENTER);

textView = (TextView) findViewById(R.id.textView);
imageView = (ImageView) findViewById(R.id.rightImage);
leftImageView = (ImageView) findViewById(R.id.leftImage);

textView.setText(text);
textView.setTextSize(textSize);
textView.setTextColor(textColor);
imageView.setImageDrawable(drawable);
leftImageView.setImageDrawable(leftDrawable);
}

public void setTextColor(int colorId) {
textView.setTextColor(colorId);
}

public void setImageDrawable(Drawable drawable) {
imageView.setImageDrawable(drawable);
}
}
attrs
<declare-styleable name="CustomRadioButton">
<attr name="TextDrawable" format="reference"></attr>
<attr name="LeftTextDrawable" format="reference"></attr>
<attr name="TextColor" format="color"></attr>
<attr name="TextSize" format="dimension"></attr>
<attr name="Text" format="string"></attr>
</declare-styleable>











  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <item>  
  4.       <shape   
  5.         android:shape="rectangle">  
  6.             <solid android:color="#FF0000" />  
  7.             <corners android:radius="30dp"/>  
  8.         </shape>  
  9.    </item>  
  10.   
  11.    <item android:left="30dp"  
  12.         >  
  13.       <shape   
  14.         android:shape="rectangle">  
  15.             <solid android:color="#FF0000" />  
  16.         </shape>  
  17.    </item>  
  18.   
  19. </layer-list>  





第一种:
现在drawable/下创建semi_round_backgroud.xml:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:color="@color/colorAccent">
    <corners
        android:radius="60dip"
        />
    <stroke
        android:width="0dp"
        android:color="@color/colorAccent" />
    <solid
        android:color="@color/colorAccent" />
</shape>


第二种:
semi2.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="@color/colorAccent" />
    <size android:height="30dp" />
    <corners
        android:bottomRightRadius="30dp"
        android:bottomLeftRadius="30dp"
        android:topRightRadius="30dp"
        android:topLeftRadius="30dp"
        />
</shape>
0 0