【问题标题】:How can i fit RelativeLayout to screen?我怎样才能使RelativeLayout适合屏幕?
【发布时间】:2013-04-17 07:32:38
【问题描述】:

我正面临RelativeLayout 屏幕外按钮显示的问题。 ImageView 不会缩放图片以使按钮可见。

我有什么:

我想要什么:

代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center_vertical"  >

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:textAppearance="@android:style/TextAppearance.Small"
        android:textColor="?android:attr/textColorTertiary"
        android:layout_centerHorizontal="true"
        />

<ru.mw.widget.DrawableImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:adjustViewBounds="true"
        android:layout_centerHorizontal="true"
        android:scaleType="centerCrop"
        />

<Button
        android:id="@+id/processButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        />

更改屏幕方向时的问题: 如果我在横向模式下使用 Arun C Thomas's method 一切正常,但在纵向模式下我有这个(图像被左/右边缘裁剪):

预期:

【问题讨论】:

  • 将 android:alignParentBottom 添加到您的按钮,并将 android:alignParentTop (both = true) 添加到您的 Textview。您目前没有为 ImageView 设置任何边界。

标签: android xml android-layout android-relativelayout


【解决方案1】:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center_vertical"  >

<TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:textColor="?android:attr/textColorTertiary"
        android:textAppearance="@android:style/TextAppearance.Small"

        android:layout_centerHorizontal="true"
        />

<ru.mw.widget.DrawableImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"
    android:layout_above="@+id/processButton"
     />

<Button
        android:id="@+id/processButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        />
</RelativeLayout>

这就是解决办法

现在要实现 更新 要求,请将上述布局添加到 res 中的 layout-land 文件夹(如果您没有在 res 中创建一个名为 layout-land 的文件夹)现在在默认 layout 文件夹中添加此 xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center"  >
<ru.mw.widget.DrawableImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:adjustViewBounds="true"
     />

<Button
    android:id="@+id/processButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:text="@string/str" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:text="@string/str"
    android:textColor="?android:attr/textColorTertiary" />

</RelativeLayout>

就是这样。

【讨论】:

  • 我希望这可以通过一种布局来实现,但你是对的,谢谢!
【解决方案2】:

尝试在按钮上添加android:layout_alignParentBottom="true"

<Button
    android:id="@+id/processButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    />

【讨论】:

    【解决方案3】:
    1. 使用对齐按钮到父底部 layout_alignParentBottom="true"
    2. ImageView 包裹在TextViewButton 之间

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center_vertical"  >
    
    <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str"
            android:textAppearance="@android:style/TextAppearance.Small"
            android:textColor="?android:attr/textColorTertiary"
            android:layout_centerHorizontal="true"
            />
    
    <Button
            android:id="@+id/processButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            />
    
    <ru.mw.widget.DrawableImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:layout_above="@+id/processButton"
            android:adjustViewBounds="true"
            android:layout_centerHorizontal="true"
            android:scaleType="centerCrop"
            />
    
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多