【问题标题】:Adjust button width according to ImageButton drawable根据 ImageButton drawable 调整按钮宽度
【发布时间】:2014-01-13 21:55:17
【问题描述】:

我有两个问题:

  1. 在布局 xml 中有一个 ImageButton,其中 drawable 是一种资源。对于此 imageButton 具有不同大小的 drawable-ldpi 到 xxhdpi,宽度会因应用程序运行的设备而异。在布局 xml 的底部,我有一个 9-patch 按钮,我希望它具有与 drawable/imageButton 相同的宽度。

  2. 如何在不同的布局 xml 中继承从 1. 到第二个 9-patch 按钮的动态?

我的尝试是用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>

【问题讨论】:

  • 请给出你需要的视图的id,你的xml不是很清楚...
  • 你能提供一个你所期待的草图吗?

标签: android xml android-layout android-relativelayout


【解决方案1】:

第一

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

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

并设置您打算用来调整按钮宽度的视图的 ID。但是您只需要使用一个 RelativeLayout 即可获得结果。你能张贴一张你想要得到的照片吗?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多