2014-09-02 67 views
0

我想在同一行中獲得3個具有相同大小的ImageButton。ImageButton Scale

我想要的東西,這就像:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/black" 
    android:orientation="vertical" 
    android:paddingBottom="40dp" 
    android:paddingTop="40dp" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal|fill_horizontal|center" 
     android:gravity="fill_horizontal" > 

     <LinearLayout 
      android:id="@+id/dummy_005" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 
     </LinearLayout> 

     <ImageButton 
      android:id="@+id/button_scan" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:background="@drawable/image_button" 
      android:focusableInTouchMode="true" 
      android:scaleType="fitCenter" 
      android: 
      android:src="@drawable/scan_symbol" /> 

     <LinearLayout 
      android:id="@+id/dummy_002" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 

     </LinearLayout> 

     <ImageButton 
      android:id="@+id/button_hitsorie" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:background="@drawable/image_button" 
      android:src="@drawable/historie_symbol" /> 

     <LinearLayout 
      android:id="@+id/dummy_003" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 
     </LinearLayout> 

     <ImageButton 
      android:id="@+id/button_2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:background="@drawable/image_button" /> 

     <LinearLayout 
      android:id="@+id/dummy_004" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 

     </LinearLayout> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/textView_result" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:textSize="@dimen/testTextSize" /> 

    </LinearLayout> 
</LinearLayout> 

的問題:我希望該按鈕會quadtra(寬度==高度)。但寬度爲「0dp」,重量爲3.但我怎麼能按鈕高度縮放?

謝謝

+0

繼承ImageButton類的子類,並在計算寬度後將高度設置爲等於寬度。然後使用您的自定義ImageButton類而不是股票。 – peshkira 2014-09-02 11:29:20

+1

@peshkira有正確的想法..請參閱我的工作示例代碼的答案。 – speedynomads 2014-09-02 12:09:26

回答

2

您需要繼承ImageButton類並將告訴它的widthMeasureSpec同時適用於寬度和高度。

public class SquareImageButton extends ImageButton { 

    public SquareImageButton(Context context) { 
     this(context, null); 
    } 

    public SquareImageButton(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public SquareImageButton(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
    } 
} 

然後在你的佈局做這樣的事情,顯然與您的自定義視圖的位置替換包名稱...

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

<com.example.domji84.quadtraimagebuttons.SquareImageButton 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1"/> 

<com.example.domji84.quadtraimagebuttons.SquareImageButton 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1"/> 

<com.example.domji84.quadtraimagebuttons.SquareImageButton 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1"/> 

</LinearLayout> 

這將始終設置自定義ImageButton的高度匹配其在屏幕上呈現時的寬度。

請注意,這不會繼承系統主題樣式,雖然有自定義視圖中設置此方法,但最好選擇自己的樣式並設置背景顏色,圖像,文本,選擇器和其他任何東西您需要...

1

我希望這會幫助你,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" 
android:weightSum="3" > 

<ImageButton 
    android:id="@+id/button_1" 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:layout_weight="1" 
    android:background="@android:drawable/btn_default" /> 

<ImageButton 
    android:id="@+id/button_2" 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:layout_weight="1" 
    android:background="@android:drawable/btn_default" /> 
    <ImageButton 
    android:id="@+id/button_3" 
    android:layout_width="80dp" 
    android:layout_height="80dp" 
    android:layout_weight="1" 
    android:background="@android:drawable/btn_default" /> 
    </LinearLayout> 
+0

這些圖像按鈕不會填滿屏幕的寬度。 – speedynomads 2014-09-02 12:08:07

+0

你應該使用gridview的相同大小的概念不是這個imagebutton – RajeshVijayakumar 2014-09-02 12:12:40

+0

當然,一個gridview會提供所需的效果,但帶來額外的開銷,因爲你必須創建一個適配器和自定義佈局,還有更多的時候膨脹渲染布局... if它只是一行三個平方的項目,自定義圖像按鈕或其他視圖是好的解決方案。 – speedynomads 2014-09-02 13:15:59

1
<?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:orientation="vertical" > 

    <ImageButton 
     android:id="@+id/imageButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/abc_ab_bottom_solid_dark_holo" /> 

    <ImageButton 
     android:id="@+id/imageButton2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:src="@drawable/abc_ab_bottom_solid_dark_holo" /> 

    <ImageButton 
     android:id="@+id/imageButton3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:src="@drawable/abc_ab_bottom_solid_dark_holo" /> 

</RelativeLayout> 
+0

這甚至試圖實現什麼?該代碼缺少行並格式不正確。 – speedynomads 2014-09-02 12:08:56