java - 在RelativeLayout中缩放的ImageView放置

标签 java android xml android-relativelayout android-imageview

我需要在RelativeLayout中放置三个元素:

  • 右侧 ImageView 中的缩放图像,具有顶部和底部边距
  • 左上角的TextEdit
  • LinearLayout 位于其余空间(标题下方、图像左侧)

ImageView 中缩放图像时会出现此问题。考虑到边距,我希望它适合整个 parent 的高度。均匀缩放以适应父级的高度后,我希望 ImageView 包裹图像的宽度。但结果是 ImageView 中有一些未填充的水平空间。

该问题看起来与已解决的问题类似 here 。但就我而言,所有使用 android:adjustViewBoundsandroid:scaleTypes 的测试都不起作用。我认为由于边距和相对布局的使用而出现问题。

以下是我的 xml 文件的摘录:

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

    <ImageView 
      android:id="@id/albums_slideshow"
      android:adjustViewBounds="true"
      android:layout_marginTop="71.33dp"
      android:layout_marginBottom="88.67dp"
      android:scaleType="centerInside"
      android:layout_height="fill_parent"
      android:layout_width="wrap_content"
      android:layout_alignParentRight="true"
      android:src="@drawable/music_img_album_noimage"/>

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="@dimen/music_module_screen_title_margin_left"
      android:layout_marginTop="@dimen/music_module_screen_title_margin_top"
      android:text="@string/music_string"
      style="@style/ScreenTitleFont"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"/>

    <LinearLayout
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_alignParentLeft="true"
      android:layout_alignTop="@id/albums_slideshow" 
      android:layout_toLeftOf="@id/albums_slideshow">

        ... 

      </LinearLayout>
 </RelativeLayout>

有人遇到过这个问题并找到答案吗?

最佳答案

这就是我想出的

enter image description here

这是 xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:orientation="vertical"
>
<TextView android:id="@+id/album_text"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
  android:layout_marginLeft="20dip" android:layout_marginTop="20dip"
  android:text="music string" android:background="@android:color/white"
/>

<LinearLayout android:id="@+id/albums_list"
  android:layout_width="wrap_content" android:layout_height="fill_parent"
  android:layout_alignParentLeft="true" android:layout_below="@id/album_text"
  android:layout_marginTop="71.33dp" 
  android:orientation="vertical" android:background="@android:color/white"
>
<TextView android:id="@+id/album_text"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
  android:layout_marginLeft="20dip" android:layout_marginTop="20dip"
  android:text="albums list"  
/>
</LinearLayout>

<ImageView android:id="@+id/albums_slideshow"    
  android:layout_height="fill_parent" android:layout_width="wrap_content"     
  android:layout_alignParentRight="true"
  android:layout_marginBottom="88.67dp"
  android:layout_alignTop="@id/albums_list" 
  android:scaleType="centerInside"
  android:layout_centerInParent="true" android:background="@android:color/darker_gray"
  android:src="@drawable/icon"/>
</RelativeLayout>

让我知道需要改进的地方

关于java - 在RelativeLayout中缩放的ImageView放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7251415/

相关文章:

java - 持久 I/O 流连接

java - Android - 如何检测父布局类型

XML 中动态节点的 SQL 查询

android - 如何创建像TextView和EditText这样的Whatsapp?

xml - 错误 : open Xml file wiht Chrome that contains spcials characters in namespaceURI

java - 如何在注释中使用不同的枚举类型?

java - 计算数组中的字母(java)

java - 检修台模型 Swing

android - 在 android 棉花糖上获取 realPath 返回 null?

android - 当 Snackbar 自行关闭时,如何通知我?