『ANDROID』android 自定义RadioButton样式

来源:互联网 发布:北京软件行业协会官网 编辑:程序博客网 时间:2024/05/23 15:45

原文地址:http://blog.csdn.net/meegomeego/article/details/8025346


<?xml version="1.0" encoding="utf-8"?>   <selector xmlns:android="http://schemas.android.com/apk/res/android">       <item       android:state_checked="false"       android:drawable="@drawable/tabswitcher_long" />       <item       android:state_checked="true"       android:drawable="@drawable/tabswitcher_short" />   </selector> 

<RadioGroup          android:gravity="center"          android:orientation="horizontal"            android:layout_width="fill_parent"             android:layout_height="wrap_content"          >          <RadioButton                 android:id="@+id/btn_0"                android:layout_width="fill_parent"                 android:layout_height="wrap_content"              android:text="最新信息"               android:textSize="17.0sp"                 android:textColor="@android:color/black"                android:gravity="center"               android:layout_weight="1"              android:checked="true"              android:button="@null"              android:background="@drawable/radio"              />            <RadioButton                 android:id="@+id/btn_1"              android:layout_width="fill_parent"                 android:layout_height="wrap_content"              android:text="在线专家"              android:textSize="17.0sp"              android:textColor="@android:color/black"                android:gravity="center"              android:layout_weight="1"              android:button="@null"              android:background="@drawable/radio"              />            <RadioButton                 android:id="@+id/btn_2"              android:layout_width="fill_parent"                 android:layout_height="wrap_content"              android:text="预约服务"              android:textSize="17.0sp"              android:textColor="@android:color/black"                android:gravity="center"              android:layout_weight="1"              android:button="@null"              android:background="@drawable/radio"              />        </RadioGroup>  

文字旁边有图标的怎么办? 

也很简单啦,只要在每个RadioButton上加android:drawableLeft="@drawable/tubiao_0"就可以了。 
另外要设置图标与文字的距离怎么办? 
有一个方法setCompoundDrawablePadding(pad)可以设置图标与文字的距离,对应的属性为android:drawablePadding。 


原文地址:http://www.iteye.com/topic/1116261#2256664


在android中实现菜单功能有多种方法。

Options Menu:用户按下menu Button时显示的菜单。

Context Menu:用户长时间按下屏幕,所显示出来的菜单也称为上下文菜单。

Submenu:子菜单。

但是有时候这些内置的菜单并不能满足我们功能,这就需要自己自定义一种菜单。接下来我说的这种就是通过TabHost与RadioGroup结合完成的菜单。这也是很常用的一种底部菜单做法。先上图:

 



 

首先看布局文件:

<?xml version="1.0" encoding="UTF-8"?>  <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"    xmlns:android="http://schemas.android.com/apk/res/android">      <LinearLayout           android:orientation="vertical"           android:layout_width="fill_parent"           android:layout_height="fill_parent">          <FrameLayout               android:id="@android:id/tabcontent"               android:layout_width="fill_parent"               android:layout_height="0.0dip"               android:layout_weight="1.0" />          <TabWidget               android:id="@android:id/tabs"               android:visibility="gone"               android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:layout_weight="0.0" />          <RadioGroup               android:gravity="center_vertical"               android:layout_gravity="bottom"               android:orientation="horizontal"               android:id="@+id/main_radio"               android:background="@drawable/maintab_toolbar_bg"               android:layout_width="fill_parent"               android:layout_height="wrap_content">              <RadioButton                   android:id="@+id/radio_button0"                   android:tag="radio_button0"                   android:layout_marginTop="2.0dip"                   android:text="@string/alarm"                   android:drawableTop="@drawable/icon_1"                   style="@style/main_tab_bottom" />              <RadioButton                   android:id="@+id/radio_button1"                   android:tag="radio_button1"                   android:layout_marginTop="2.0dip"                   android:text="@string/message"                   android:drawableTop="@drawable/icon_2"                   style="@style/main_tab_bottom" />              <RadioButton                   android:id="@+id/radio_button2"                   android:tag="radio_button2"                   android:layout_marginTop="2.0dip"                   android:text="@string/photo"                   android:drawableTop="@drawable/icon_3"                   style="@style/main_tab_bottom" />              <RadioButton                   android:id="@+id/radio_button3"                   android:tag="radio_button3"                   android:layout_marginTop="2.0dip"                   android:text="@string/music"                   android:drawableTop="@drawable/icon_4"                   style="@style/main_tab_bottom" />              <RadioButton                   android:id="@+id/radio_button4"                   android:tag="radio_button4"                   android:layout_marginTop="2.0dip"                   android:text="@string/setting"                   android:drawableTop="@drawable/icon_5"                   style="@style/main_tab_bottom" />          </RadioGroup>      </LinearLayout>  </TabHost>  

需要注意的是,如果用TabHost这个控件,其中有几个ID是必须这么写的,android:id="@android:id/tabhost   ;android:id="@android:id/tabcontent" ;android:id="@android:id/tabs" ;之所以要这么写是因为在TabHost这个类中。需要实例化上述这个ID的控件。看源码:

 

在TabActivity中有么个方法:

public void onContentChanged() {         super.onContentChanged();         mTabHost = (TabHost) findViewById(com.android.internal.R.id.tabhost);           if (mTabHost == null) {             throw new RuntimeException(                     "Your content must have a TabHost whose id attribute is " +                     "'android.R.id.tabhost'");         }         mTabHost.setup(getLocalActivityManager());     }       private void ensureTabHost() {         if (mTabHost == null) {             this.setContentView(com.android.internal.R.layout.tab_content);         }     }

当内容发生改变时它会调用这个方法,来更新列表或者其他视图,而这个方法中需要实例化TabHost,所以必须通过ID为tabhost实例化。

 

再看看TabHost这个类中,

public void setup() {        mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);        if (mTabWidget == null) {            throw new RuntimeException(                    "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");        }          // KeyListener to attach to all tabs. Detects non-navigation keys        // and relays them to the tab content.        mTabKeyListener = new OnKeyListener() {            public boolean onKey(View v, int keyCode, KeyEvent event) {                switch (keyCode) {                    case KeyEvent.KEYCODE_DPAD_CENTER:                    case KeyEvent.KEYCODE_DPAD_LEFT:                    case KeyEvent.KEYCODE_DPAD_RIGHT:                    case KeyEvent.KEYCODE_DPAD_UP:                    case KeyEvent.KEYCODE_DPAD_DOWN:                    case KeyEvent.KEYCODE_ENTER:                        return false;                  }                mTabContent.requestFocus(View.FOCUS_FORWARD);                return mTabContent.dispatchKeyEvent(event);            }          };          mTabWidget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {            public void onTabSelectionChanged(int tabIndex, boolean clicked) {                setCurrentTab(tabIndex);                if (clicked) {                    mTabContent.requestFocus(View.FOCUS_FORWARD);                }            }        });          mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);        if (mTabContent == null) {            throw new RuntimeException(                    "Your TabHost must have a FrameLayout whose id attribute is "                            + "'android.R.id.tabcontent'");        }    }

这个方法,是在增加选项卡之前由系统调用。在这个方法中需要通过tabs 这个ID实例化一个TabWidget,通过tabcontent这个ID实例化一个FrameLayout,用来放置选项卡内容。所以这两个ID也是固定的。

 

在上述布局文件中隐藏了系统默认的Widget,取而代之的是带有图片的Button。

 

看一下主要代码:

package com.iteye.androidtoast;    import android.app.TabActivity;  import android.content.Intent;  import android.os.Bundle;  import android.widget.RadioGroup;  import android.widget.RadioGroup.OnCheckedChangeListener;  import android.widget.TabHost;    public class MainActivity extends TabActivity implements OnCheckedChangeListener{      /** Called when the activity is first created. */      private TabHost mHost;      private RadioGroup radioderGroup;      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.maintabs);          //实例化TabHost          mHost=this.getTabHost();                    //添加选项卡          mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE")                      .setContent(new Intent(this,OneActivity.class)));          mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO")                  .setContent(new Intent(this,TwoActivity.class)));          mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE")                  .setContent(new Intent(this,ThreeActivity.class)));          mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR")                  .setContent(new Intent(this,FourActivity.class)));          mHost.addTab(mHost.newTabSpec("FIVE").setIndicator("FIVE")                  .setContent(new Intent(this,FiveActivity.class)));                    radioderGroup = (RadioGroup) findViewById(R.id.main_radio);          radioderGroup.setOnCheckedChangeListener(this);      }      @Override      public void onCheckedChanged(RadioGroup group, int checkedId) {          switch(checkedId){          case R.id.radio_button0:              mHost.setCurrentTabByTag("ONE");              break;          case R.id.radio_button1:              mHost.setCurrentTabByTag("TWO");              break;          case R.id.radio_button2:              mHost.setCurrentTabByTag("THREE");              break;          case R.id.radio_button3:              mHost.setCurrentTabByTag("FOUR");              break;          case R.id.radio_button4:              mHost.setCurrentTabByTag("FIVE");              break;          }             }  }

原文地址:http://www.cnblogs.com/winxiang/archive/2012/10/30/2746332.html

通过代码设置radiobutton不同方位图标的两种方法


更换radiobutton中的图片在xml中很好设置,但对于初学者如何在代码中设置还是不容易找的。没法子,通过看原版api找到两个方法,setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds。

下面交给大家方法。

第一个方法:setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)

api原文为:

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

意思大概就是:可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为null。图标的宽高将会设置为固有宽高,既自动通过getIntrinsicWidth和getIntrinsicHeight获取。——笔者翻译

 button = (RadioButton) group.getChildAt(i);2                 Resources res = TabTest.this.getResources();3                 Drawable myImage = res.getDrawable(R.drawable.home);4                 button.setCompoundDrawablesWithIntrinsicBounds(null, myImage, null, null);

如图第一个按钮:

  


第二种方法:setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)

api原文为:

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.

意思大概就是:可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为null。但是Drawable必须已经setBounds(Rect)。意思是你要添加的资源必须已经设置过初始位置、宽和高等信息。——笔者翻译

这下就明白了,这个方法要先给Drawable设置setBounds(x,y,width,height);

x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 width:组件的长度 height:组件的高度。

如代码:

1                 Resources res = TabTest.this.getResources();2                 Drawable myImage = res.getDrawable(R.drawable.home);3                 myImage.setBounds(1, 1, 100, 100);4                 button.setCompoundDrawables(null, myImage, null, null);

只要调整好宽和高。效果也是一样的。这个方法的好处就是不按比例,宽高可以打破原有的大小及比例!如图,我调的y轴有点不对齐。

 

总结:radiobutton设置不同方位的图标的方法有以上两种,如果想手动设置大小的话就要用setCompoundDrawables,事先要给Drawable设置setBounds。

        如果按照原有比例大小显示图片就使用setCompoundDrawablesWithIntrinsicBounds




0 0
原创粉丝点击