【问题标题】:android : Relative layout width wrap_contentandroid : 相对布局宽度 wrap_content
【发布时间】:2012-07-20 20:20:57
【问题描述】:

我有以下布局:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" >

    <ImageView
        android:id="@+id/personal_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/dom_logo_new" />

    <ImageView
        android:id="@+id/round_corner_top_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/corner_white_left_top" />

    <ImageView
        android:id="@+id/round_corner_top_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/corner_white_right_top" />

    <ImageView
        android:id="@+id/round_corner_bottom_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/corner_white_left_bottom" />

    <ImageView
        android:id="@+id/round_corner_bottom_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/corner_white_right_bottom" />

</RelativeLayout>

有 1 个主图像(personal_image)和 4 个小图像。

我需要在屏幕的中心水平显示主图像(personal_image),每个角有 4 个图像(左上角、右上角、左下角、右下角)。但是RelativeLayout 具有所有屏幕的宽度和高度。

如何将RelativeLayout 的width 和height 设置为personal_image 的width 和height??

【问题讨论】:

    标签: android imageview width android-relativelayout


    【解决方案1】:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" >
    
        <ImageView
            android:id="@+id/personal_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher" />
    
        <ImageView
            android:id="@+id/round_corner_top_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ic_launcher" />
    
        <ImageView
            android:id="@+id/round_corner_top_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ic_launcher" />
    
        <ImageView
            android:id="@+id/round_corner_bottom_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:src="@drawable/ic_launcher" />
    
        <ImageView
            android:id="@+id/round_corner_bottom_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:src="@drawable/ic_launcher" />
    
        <ImageView
            android:id="@+id/round_corner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/ic_launcher" />
    
    </RelativeLayout>
    </LinearLayout>
    

    当您使用包装内容时,您不必担心不同的屏幕尺寸。它会自行管理。

    【讨论】:

    • 它不起作用..小图像设置相对布局最大宽度和高度
    【解决方案2】:

    我建议您使用9-patch 而不是角落图像,这是 Android 解决此类问题的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      相关资源
      最近更新 更多