如何缩放按钮高度?

时间:2014-09-02 11:26:34

标签: android android-layout scale android-imagebutton android-layout-weight

我想在同一行中获得3 ImageButtons

我想要这样的东西:

<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>

我希望按钮为quadtrawidth == height)。但宽度为0dp且重量为:3。但我怎样才能缩放buttonsheight

3 个答案:

答案 0 :(得分: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 :(得分: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>

答案 2 :(得分: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>