【问题标题】:ImageView set within PercentRelativeLayout won't show up在 PercentRelativeLayout 中设置的 ImageView 不会显示
【发布时间】:2017-01-06 16:38:13
【问题描述】:

我尝试使用 PercentRelativeLayout 中包含的不同图像创建一个复杂的布局。

实际上我无法显示 ImageView 我想将其宽度设置为总宽度(父宽度)的百分比。

下面是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_home_page"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.avescera.remindme.HomePageActivity"
    tools:showIn="@layout/app_bar_home_page"
    android:foregroundGravity="center_vertical"
    android:gravity="center_horizontal">

    <android.support.percent.PercentRelativeLayout
        android:id="@+id/RLayoutHPLoan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">

        <ImageView
            android:id="@+id/imgHPLoanAmnt"
            app:srcCompat="@drawable/ic_money_bar"
            app:layout_widthPercent="40%"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignStart="@id/imgHPGreenArrow" />

        <ImageView
            android:id="@+id/imgHPGreenArrow"
            app:srcCompat="@drawable/ic_green_arrow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true" />

        <ImageView
            android:id="@+id/imgHPLoanBtn"
            app:srcCompat="@drawable/ic_add_loan_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_centerVertical="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:text="@string/home_loan_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:id="@+id/txtVHPLoanTitle"
            android:textColor="#FFFFFF"
            android:textAllCaps="true"
            android:textSize="36sp"
            android:textStyle="normal|bold" />

    </android.support.percent.PercentRelativeLayout>

    <android.support.percent.PercentRelativeLayout
        android:id="@+id/RLayouHPBorrow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_marginTop="16dp"
        android:layout_below="@id/RLayoutHPLoan">

    <ImageView
        app:srcCompat="@drawable/ic_red_arrow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imgHPRedArrow"
        android:adjustViewBounds="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

        <ImageView
            android:id="@+id/imgHPBorrowBtn"
            app:srcCompat="@drawable/ic_add_borrow_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="16dp" />

        <TextView
            android:text="@string/home_borrow_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:id="@+id/txtVHPBorrowTitle"
            android:textColor="#FFFFFF"
            android:textAllCaps="true"
            android:textSize="36sp"
            android:textStyle="normal|bold" />

    </android.support.percent.PercentRelativeLayout>

</LinearLayout>

以及我得到的屏幕:

在左上角,您看到(进入红色方块)我的 ImageView 没有显示。 我要显示的图像是一个正方形,里面有一个图像(全部都是图像)。

我希望看到的(我知道它与我正在构建的不完全相同:)),绿色箭头下方的灰色方块(里面有钱)必须显示在我的箭头顶部(不介意改变):

【问题讨论】:

    标签: android android-layout android-imageview android-percentrelativelayout


    【解决方案1】:

    使用 PercentRelativeLayout 是父级

    并将 PercentRelativeLayout 作为子级

    使用 app:layout_aspectRatio="150%"

    例如

    <android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
    android:background="@color/colorWhite"
                    android:layout_margin="5dp"
                   >
    
        <android.support.percent.PercentRelativeLayout
            app:layout_aspectRatio="90%"
            app:layout_heightPercent="50%"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">
    
    
    
            <ImageView
                android:id="@+id/iv_writers_image"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:src="@drawable/error_image"
                android:adjustViewBounds="true"
                android:scaleType="fitXY"
                app:layout_heightPercent="70%"
                app:layout_widthPercent="100%"
                />
    
            <TextView
                android:id="@+id/tv_writers_name"
                app:layout_heightPercent="20%"
                app:layout_widthPercent="100%"
                android:layout_below="@+id/iv_writers_image"
                android:layout_centerHorizontal="true"
    
    
                android:maxLines="1"
                android:text="TextView"
                android:gravity="center"
                android:textColor="@color/colorBlack"
                android:textSize="18dp"/>
    
        </android.support.percent.PercentRelativeLayout>
    </android.support.percent.PercentRelativeLayout>
    

    【讨论】:

    • 如果我没有为 RelativeLayout 填充 android:layout_width 和 android:layout_height,我会收到错误消息,告诉我必须填充它们。事实上,如果我只为 RelativeLayout 放置 aspectRatio 和 widthPercent 它是行不通的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多