2014-09-02 69 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