2010-11-23 126 views
3

我有這些單選按鈕,他們需要android:width="X"android:height"X"但是,我不知道如何設置這些屬性,以便它們適應不同的屏幕大小。如何設置單選按鈕的寬度以適應屏幕尺寸? (android)

下面是我用我的按鈕代碼:

<RadioGroup android:layout_width="fill_parent" 
     android:layout_height="50px" android:orientation="horizontal" 
     android:checkedButton="@+id/first" android:id="@+id/states"> 

       <RadioButton android:id="@+id/first" 
        android:background="@drawable/button_radio" android:width="50px" 
        android:height="50px" /> 

       <RadioButton android:id="@+id/second" 
        android:background="@drawable/button_radio" android:width="50px" 
        android:height="50px" /> 

       <RadioButton android:id="@+id/third" 
        android:background="@drawable/button_radio" android:width="50px" 
        android:height="50px" /> 

       <RadioButton android:id="@+id/fourth" 
        android:background="@drawable/button_radio" android:width="50px" 
        android:height="50px" /> 

       <RadioButton android:id="@+id/fifth" 
        android:background="@drawable/button_radio" android:width="50px" 
        android:height="50px" /> 

    </RadioGroup> 

我試圖把單選按鈕的錶行,但隨後他們就無法正常運行。當我點擊一個,它選擇了它,但是當我點擊另一個時沒有取消選擇它。

我試過用android:layout_width代替android:width,但是他們沒有顯示。

我也嘗試過使用dip,但是沒有顯示按鈕。

我還應該補充說我使用的是drawables而不是stock單選按鈕。我不知道這是否會有所作爲,但可繪製的尺寸都是相同的(50px x 50px)。

有誰知道我如何設置這些高度和寬度,以便他們將自己調整到不同的屏幕尺寸?

回答

7

使用dp而不是px設置高度和寬度。有關這些單元的更多信息,請參閱本文的答案。 What is the difference between "px", "dp", "dip" and "sp" on Android?

我還建議你在支持多屏的情況下熟悉Android的documentation

好的,所以我花了一點時間來掀起一個針對Android 1.5的快速示例。

你可以找到source here

總之,你需要採取以下步驟:

將您的圖像res/drawable。你應該有至少4個圖標:按下&未選中,按下&經過,未按&未經檢查,未按&經過

創建RES /繪製一個selector型佈局。這裏是我的:

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

然後設置好你的RadioGroup像這樣:

<RadioGroup android:layout_width="fill_parent" 
    android:layout_height="50dp" 
    android:orientation="horizontal" 
    android:checkedButton="@+id/first"> 
    <RadioButton android:id="@+id/first" 
     android:width="50dp" 
     android:height="50dp" 
     android:button="@drawable/button_radio"/> 
    <RadioButton android:id="@+id/second" 
     android:width="50dp" 
     android:height="50dp" 
     android:button="@drawable/button_radio"/> 
    <RadioButton android:id="@+id/third" 
     android:width="50dp" 
     android:height="50dp" 
     android:button="@drawable/button_radio"/> 
    <RadioButton android:id="@+id/fourth" 
     android:width="50dp" 
     android:height="50dp" 
     android:button="@drawable/button_radio"/> 
</RadioGroup> 

我指定50dp的尺寸作爲我可繪製的尺寸爲50像素X 50像素。另請注意,我正在設置android:button而不是android:background

這裏是上述項目的簽署APK:Custom RadioButton(注:這是針對SDK版本4級所以它不會要求SD和電話的權限)

這應該給了以下結果: Image of Custom RadioButton

希望這會有所幫助。

+0

對不起,我應該說我嘗試了浸,但後來沒有顯示按鈕。我會將其添加到OP。我還會添加一個事實,即使用drawable而不是標準按鈕。 – NotACleverMan 2010-11-23 18:20:30

+1

如果你的按鈕是實際的圖像,然後創建3個不同的版本,並將它們放在`res/drawable-ldpi`,`res/drawable-mdpi`,`res/drawable-hdpi`文件夾中。你爲什麼要指定像素大小呢? – Thomas 2010-11-23 18:22:08

8

我建議你嘗試用你的radioGroup中的以下內容:

  <RadioButton android:id="@+id/first" 
       android:background="@drawable/button_radio" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:weight=1 /> 

      <RadioButton android:id="@+id/second" 
       android:background="@drawable/button_radio" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:weight=1 /> 

      <RadioButton android:id="@+id/third" 
       android:background="@drawable/button_radio" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:weight=1 /> 

      <RadioButton android:id="@+id/fourth" 
       android:background="@drawable/button_radio" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:weight=1 /> 

      <RadioButton android:id="@+id/fifth" 
       android:background="@drawable/button_radio" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:weight=1 /> 

</RadioGroup> 

這樣,所有的單選按鈕,將佔據相等空間。此外,它會根據屏幕大小而改變。

相關問題