验证码对话框布局(Android)

时间:2014-04-16 05:56:59

标签: android xml android-layout

我正在为Android应用添加验证码对话框,并且难以正确调整其大小。验证码图像是120x50,我希望它按比例放大以适应对话框的宽度。使用我当前的XML,它符合宽度,但会覆盖下面的按钮。

enter image description here

我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content">
        <ImageView android:layout_width="fill_parent"
                   android:layout_gravity="center"
                   android:id="@+id/captchaImage"
                   android:adjustViewBounds="true"
                   android:scaleType="fitXY"
                   android:layout_height="fill_parent"/>
    </LinearLayout>
    <EditText android:layout_width="fill_parent"
              android:id="@+id/captchaText"
              android:layout_height="wrap_content"/>
    <LinearLayout android:layout_width="match_parent"
                  android:orientation="horizontal"
                  android:layout_height="wrap_content">
        <TableLayout android:layout_width="0dp"
                    android:layout_weight="0.5"
                    android:layout_height="wrap_content">
            <Button android:layout_width="fill_parent"
                    android:text="Cancel"
                    android:id="@+id/cancelCaptcha"
                    android:layout_height="wrap_content"/>
        </TableLayout>
        <TableLayout android:layout_width="0dp"
                    android:layout_weight="0.5"
                    android:layout_height="wrap_content">
            <Button android:layout_width="fill_parent"
                    android:text="Submit"
                    android:id="@+id/submitCaptcha"
                    android:layout_height="wrap_content"/>
        </TableLayout>
    </LinearLayout>
</LinearLayout>

任何想法如何让120x50图像缩放到对话框的宽度而不覆盖它下面的任何东西?

提前致谢, MrGrinst

1 个答案:

答案 0 :(得分:1)

您无需将每个视图包装在另一个LinearLayoutTableLayout中,您可以利用权重让自己调整大小。试一试:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical" >

    <ImageView android:id="@+id/captchaImage"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:scaleType="fitXY" />

    <EditText android:id="@+id/captchaText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

        <Button android:id="@+id/cancelCaptcha"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Cancel" />

        <Button android:id="@+id/submitCaptcha"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Submit" />

    </LinearLayout>

</LinearLayout>