動態設定Button圖片大小
阿新 • • 發佈:2019-02-06
動態設定Button大小及點選事件:
在佈局中寫下這段程式碼:新建立一個class 名字叫MyRadioButton 繼承RdioButton
package duan.com.homework.helper; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.RadioButton; import duan.com.homework.R;然後在res下建立attrs:/** * Created by Administrator on 2016/9/22. */ public class MyRadioButton extends RadioButton { public MyRadioButton(Context context) { super(context); } public MyRadioButton(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyRadioButton); //drawableSize = a.getDimensionPixelSize(R.styleable.MyRadioButton_rbDrawableTopSize, 50); Drawable drawableTop = a.getDrawable(R.styleable.MyRadioButton_rbDrawableTop); a.recycle(); setCompoundDrawablesWithIntrinsicBounds(null,drawableTop,null,null); } public MyRadioButton(Context context,AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) { super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom); if (top!=null){ top.setBounds(0,0,50,50); } setCompoundDrawables(left,top,right,bottom); } }
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyRadioButton"> <attr name="rbDrawableTopSize" format="dimension"/> <attr name="rbDrawableTop" format="reference"/> </declare-styleable> </resources>這是一個按鈕 點選切換圖示 我就寫了一個 其他自己寫就OK 其實很簡單
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/timeline_toolbar_btn_news_selected"></item> <item android:drawable="@drawable/timeline_toolbar_btn_news_normal" android:state_checked="false"></item> </selector>
字型顏色
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:color="#168DFE"></item> <item android:state_checked="false" android:color="#777777"></item> </selector>然後在主佈局中,我的主佈局就是RadioGroup中套 RadioButton
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">上面的app和tools一定要寫出來 這個就自動生成就好了,然後在Button按鈕中寫
<RadioGroup android:id="@+id/radioGroup_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"
// 橫向一定要寫 不然會沒有東西 android:orientation="horizontal"> <duan.com.homework.helper.MyRadioButton android:id="@+id/button_news" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"
//這步就是去掉Button前面的圓圈 android:button="@null"
//選中的圖片 預設選中 因為是第一個按鈕 android:checked="true"
//讓文字居中 在Button的下面 android:gravity="center" android:text="新聞"
//這步最重要 不能自動生成 需要手動來寫 就是把之前寫的attrs中的rbDrawableTop拿來使用
後面就是drawable資源裡面的選中和非選中 按鈕的切換 app:rbDrawableTop="@drawable/button_color_news" />是不是圖片變小了
如果不是你想要的 你還可以在MyRadioButton中修改
下面就是我主佈局裡面的程式碼 我是模仿騰訊底部做的 下一步做頂部導航
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <RadioGroup android:id="@+id/radioGroup_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal"> <duan.com.homework.helper.MyRadioButton android:id="@+id/button_news" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:button="@null" android:checked="true" android:gravity="center" android:text="新聞" android:textColor="@drawable/button_text_color" app:rbDrawableTop="@drawable/button_color_news" /> <duan.com.homework.helper.MyRadioButton android:id="@+id/button_recommend" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:button="@null" android:checked="false" android:gravity="center" android:text="推薦" android:textColor="@drawable/button_text_color" app:rbDrawableTop="@drawable/button_color_recommend" /> <duan.com.homework.helper.MyRadioButton android:id="@+id/button_live" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:button="@null" android:checked="false" android:gravity="center" android:text="直播" android:textColor="@drawable/button_text_color" app:rbDrawableTop="@drawable/button_color_live" /> <duan.com.homework.helper.MyRadioButton android:id="@+id/button_me" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:button="@null" android:checked="false" android:gravity="center" android:text="我" android:textColor="@drawable/button_text_color" app:rbDrawableTop="@drawable/button_color_me" /> </RadioGroup>
<FrameLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/radioGroup_main" ></FrameLayout></RelativeLayout>