繁体   English   中英

不显示ImageView

[英]ImageView isn't displayed

我有一个布局,但未显示具有@id/category_starred_icon的ImageView,预期的行为是将带有2个TextView的LinearLayout扩展到所有可用空间,但是未显示ImageView

<?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="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/borders" >

    <View
        android:id="@+id/category_color"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@color/awazari_blue" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/category_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textIsSelectable="false"
                android:textSize="18sp"
                android:paddingLeft="5dp"
                android:paddingTop="5dp"
                android:paddingRight="5dp"
                android:textColor="@android:color/black"
                android:hint="@string/app_name"  />

            <TextView
                android:id="@+id/category_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textIsSelectable="false"
                android:maxLines="3"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:textColor="@color/awazari_medium_gray"
                android:ellipsize="end"
                android:hint="@string/description" />
        </LinearLayout>

        <ImageView
            android:id="@+id/category_starred_icon"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:src="@drawable/unstarred"
            android:contentDescription="@string/app_name"
            android:layout_gravity="right" />

    </LinearLayout>
</LinearLayout>

使用下面的代码,它将完全给您您想要的东西:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"

android:padding="6dip">

<LinearLayout
    android:orientation="vertical"

    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"                   
        android:gravity="center_vertical"
        android:text="First Textview" />

    <TextView  
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"             
        android:singleLine="true"
        android:ellipsize="marquee"
        android:text="Second Textview" />

</LinearLayout>

<ImageView
    android:id="@+id/icon"        
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="6dip"        
    android:src="@drawable/ic_launcher" />

</LinearLayout>

这是输出:

在此处输入图片说明

希望这对你有帮助。

我不确定没有截图或描述的确切含义,但是您的问题是您的ImageView LienarLayout horizontal orientationLienarLayout ,并且具有另一个widthmatch_parent子级LinearLayouthorizontal orientationmatch_parent ,因此ImageView没有空间

<LinearLayout
    android:orientation="horizontal"    <!-- This is the ImageViews parent  -->
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"         <!-- This child is taking up all of the width  -->
        android:layout_height="wrap_content">

您可以根据需要执行不同的操作,例如删除其中一个LinearLayout或使其vertical orientation或使用RelativeLayout 但是我不知道你到底想要什么,所以很难给出一个完整的答案

编辑

您可以查看它是否满足您的需求。 没有屏幕截图或更好的描述,很难确切知道您想要什么。 但是你可以根据自己的喜好调整它

<RelativeLayout
    android:layout_width="match_parent"     //changed this to a RelativeLayout
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/ll"
        android:orientation="vertical"
        android:layout_width="wrap_content"    // changed the width here
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/category_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textIsSelectable="false"
            android:textSize="18sp"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:paddingRight="5dp"
            android:textColor="@android:color/black"
            android:text="Text"  />

        <TextView
            android:id="@+id/category_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textIsSelectable="false"
            android:maxLines="3"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:ellipsize="end" 
            android:text="Text Again"/>
    </LinearLayout>

    <ImageView
        android:id="@+id/category_starred_icon"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:src="@drawable/blue_box"
        android:layout_gravity="right"
        android:layout_alignParentRight="true" />

</LinearLayout>

请注意,我只是更改了TextView的文本,颜色和背景,以便可以在编辑器上显示它

您的ImageView已隐藏,因为您同时使用android:orientation="horizontal"android:layout_width="match_parent"进行布局。

它已将您的ImageView隐藏在看不见的内部LinearLayout的右侧。

您可以使用android:weight=1通过LinearLayout填充除ImageView之外的所有可用空间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM