当父布局内容溢出时,调整ImageView的ViewBounds

时间:2017-03-16 10:50:22

标签: android android-layout view adjustviewbounds

我是一个垂直的LinearLayout,其中包含以下小部件,其中LinearLayout的重力设置为" center":

  • ImageView的
  • TextInputLayout
  • TextInputLayout
  • 按钮

这通常很好看,当在较小的屏幕设备上时,底部的按钮不在视野范围内。当我为TextInputLayouts显示错误时,这甚至会变得更糟。

屏幕截图正常屏幕:

On Normal Screens

截图小屏幕:

On Small Screens

问题:

我想要的是,在小屏幕上,ImageView应调整大小以适应所有视图,而在更大的屏幕上,ImageView应保持其最大可用大小,而所有内容都显示在中心(如屏幕截图1)。

任何帮助将不胜感激! :)

SOURCE XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/input_view_margin"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_transaction_history_big" />

    <TextView
        style="@style/ReportHeading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txt_transaction_history" />

    <android.support.v4.widget.Space
        android:layout_width="wrap_content"
        android:layout_height="40dp" />

    <TextInputLayout
        android:id="@+id/wrapperFromDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextInputEditText
            android:id="@+id/edtFromDate"
            style="@style/InputLayoutEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:hint="@string/hint_from_date" />
    </TextInputLayout>

    <TextInputLayout
        android:id="@+id/wrapperToDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <TextInputEditText
            android:id="@+id/edtToDate"
            style="@style/InputLayoutEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:hint="@string/hint_to_date" />
    </TextInputLayout>

    <Button
        android:id="@+id/btnSubmit"
        style="@style/PrimaryButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:text="@string/action_submit" />
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

您可以将ScrollView添加到您的XML中,以便用户能够看到您的按钮

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/input_view_margin"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_transaction_history_big" />

    <TextView
        style="@style/ReportHeading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txt_transaction_history" />

    <android.support.v4.widget.Space
        android:layout_width="wrap_content"
        android:layout_height="40dp" />

    <TextInputLayout
        android:id="@+id/wrapperFromDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextInputEditText
            android:id="@+id/edtFromDate"
            style="@style/InputLayoutEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:hint="@string/hint_from_date" />
    </TextInputLayout>

    <TextInputLayout
        android:id="@+id/wrapperToDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <TextInputEditText
            android:id="@+id/edtToDate"
            style="@style/InputLayoutEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:hint="@string/hint_to_date" />
    </TextInputLayout>

    <Button
        android:id="@+id/btnSubmit"
        style="@style/PrimaryButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:text="@string/action_submit" />
</LinearLayout>
</ScrollView>

答案 1 :(得分:0)

提供视图权重并设置宽度高度以匹配父级布局的父级,为子视图提供权重。记住将高度设置为0dp,将宽度设置为0dp,以便可以根据重量进行调整。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/input_view_margin"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:srcCompat="@drawable/ic_transaction_history_big" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            style="@style/ReportHeading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/txt_transaction_history" />

        <android.support.v4.widget.Space
            android:layout_width="wrap_content"
            android:layout_height="40dp" />

        <TextInputLayout
            android:id="@+id/wrapperFromDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextInputEditText
                android:id="@+id/edtFromDate"
                style="@style/InputLayoutEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:hint="@string/hint_from_date" />
        </TextInputLayout>

        <TextInputLayout
            android:id="@+id/wrapperToDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp">

            <TextInputEditText
                android:id="@+id/edtToDate"
                style="@style/InputLayoutEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:hint="@string/hint_to_date" />
        </TextInputLayout>

        <Button
            android:id="@+id/btnSubmit"
            style="@style/PrimaryButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="35dp"
            android:text="@string/action_submit" />

    </LinearLayout>

</LinearLayout>

它会解决你的问题,让我知道它是否有效。祝你好运!

答案 2 :(得分:0)

使用ConstraintLayout作为“弹性视图”,这样您就可以为元素设置灵活的约束。

PS:你可以在android studio中轻松测试你的实现,当你扩展时,减少预览/设计中的屏幕尺寸。