根据图像按钮可绘制对象调整按钮宽度



我有两个问题:

  1. 在布局 xml 中,有一个ImageButton可绘制对象是资源。对于此图像按钮,具有不同大小的可绘制 ldpi 到 xxhdpi,宽度因应用程序正在运行的设备而异。在布局 xml 的底部,我有一个 9 个补丁按钮,我希望它的宽度与可绘制对象/图像按钮相同。

  2. 我将如何继承动态从 1. 到不同布局 xml 中的第二个 9 补丁按钮?

我的尝试是用wrap_content做一个RelativeLayout,它很好地包裹了一切。当我将buttonStart宽度设置为 match_parent 时,它使用整个屏幕,而不是 UIWrapper 的宽度。任何帮助,不胜感激。感谢!

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_activity_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:focusable="true"
    android:screenOrientation="portrait"
    tools:context=".MainActivity" >
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/UIWrapper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="62dp" >
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/imageWrapper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" >
            <ImageButton
                android:id="@+id/imageButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="false"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:background="?android:attr/selectableItemBackground"
                android:src="@drawable/my_gfx" />
            <TextView
                android:id="@+id/textViewTest"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_gravity="center_horizontal|top"
                android:clickable="false"
                android:shadowColor="@color/LightYellow"
                android:shadowDx="0.0"
                android:shadowDy="0.0"
                android:shadowRadius="24"
                android:text="Some Text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/DarkBlue"
                android:textSize="48dp" />
        </RelativeLayout>
        <TextView
            android:id="@+id/T0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/imageWrapper"
            android:layout_below="@+id/imageWrapper"
            android:layout_marginTop="10dp"
            android:text="@string/label"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/FloralWhite" />
        <RadioGroup
            android:id="@+id/T1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/T0"
            android:layout_below="@+id/T0" >
            <RadioButton
                android:id="@+id/radioButton0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:enabled="true"
                android:singleLine="true"
                android:text="@string/rd_0"
                android:textColor="@color/White" />
            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:enabled="true"
                android:text="@string/rd1"
                android:textColor="@color/FloralWhite" />
        </RadioGroup>
        <Button
            android:id="@+id/buttonStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="14dp"
            android:background="@drawable/default_button"
            android:text="@string/btn_start"
            android:textColor="@color/FloralWhite" />
    </RelativeLayout>
</RelativeLayout>

First

要获得相同的宽度,请使用:

 android:id="@+id/buttonStart"
 android:layout_alignRight="@+id/..."
 android:layout_alignLeft="@+id/..."

,然后设置要用于调整按钮宽度大小的视图的 ID。但是您只需要使用一个相对布局即可获得结果。你能发布你想要得到的东西的照片吗?

  1. 我不得不说:你使用了太多嵌套的相对布局。它不是一个明确和常见的。
  2. 您不需要重复该属性 xmlns:android="http://schemas.android.com/apk/res/android",就一次,在第一个元素。