【问题标题】:Android Layout Text at Left and Image at RightAndroid 布局左侧的文本和右侧的图像
【发布时间】:2013-04-05 01:45:43
【问题描述】:

我正在尝试制作一个列表,其中图像始终位于布局的右侧。同时,如果没有图像,我希望主题文本填满整个布局。

这是我希望实现的
当图像被隐藏时

与图像

电流输出:
文字前面的图片。

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:padding="6dip" android:layout_height="?android:attr/listPreferredItemHeight">
    <TextView
        android:id="@+id/subject"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:text="Apple Orange Apple Orange Apple Orange Apple Orange Apple Orange Apple Orange"/>
    <TextView
        android:id="@+id/cdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true" 
        android:textColor="#8000FF"       
        android:text="Date" />
    <ImageView  
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"      
        android:layout_alignParentRight="true" 
        android:layout_alignRight="@id/subject"
        android:src="@drawable/icon" />
</RelativeLayout>

【问题讨论】:

    标签: android xml android-relativelayout android-imageview


    【解决方案1】:

    试试这个...干杯:)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:padding="6dip" android:layout_height="?android:attr/listPreferredItemHeight">
    
    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" 
        android:src="@drawable/icon" />
    
    <TextView
        android:id="@+id/subject"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:text="Apple Orange Apple Orange Apple Orange Apple Orange Apple Orange Apple Orange"
        android:layout_toLeftOf="@id/icon"/>
    <TextView
        android:id="@+id/cdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true" 
        android:textColor="#8000FF"       
        android:text="Date" />
    

    【讨论】:

    • 一种更有效的方法是使用带有复合可绘制对象的文本视图。
    【解决方案2】:

    你应该这样做:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:padding="6dip" >
    
        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@drawable/mark_ok" />
    
        <LinearLayout
            android:id="@+id/leftblock"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/subject"
            android:layout_alignTop="@+id/subject"
            android:layout_toLeftOf="@+id/icon"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/subject"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Apple Orange Apple Orange Apple Orange Apple Orange Apple Orange Apple Orange" />
    
            <TextView
                android:id="@+id/cdate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Date"
                android:textColor="#8000FF" />
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

    • 你能说出你“应该”的理由吗?清洁工?
    • 在包含2个块的相对布局中,如果指定:layout_toLeftOf="",则左侧块永远不会与右侧重叠
    • “@+”很重要,因为它指的是一个尚未创建的视图:)
    猜你喜欢
    • 2016-12-29
    • 2022-01-19
    • 2016-03-11
    • 1970-01-01
    • 2016-07-11
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    相关资源
    最近更新 更多