【问题标题】:XML android layout hiding iconXML android布局隐藏图标
【发布时间】:2014-07-22 11:23:44
【问题描述】:

我正在编写一个 Android 应用程序,但遇到了一件奇怪的事情。也就是说,它是我的图标的可见性。它应该在屏幕的右侧。

当我把它放在左边时它是好的,它是这样可见的:

但是当我把它正确的时候,它看起来像这样:

我的代码如下所示:

   <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="@dimen/space_medium" >

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginBottom="15dp" >

            <TextView
                android:id="@+id/_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:layout_marginLeft="10dp"
                android:text="@string/title"
                android:textColor="#666666" 
                android:textSize="20sp"/>

            <ImageView
                android:id="@+id/image"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:gravity="top"
                android:alpha=".50"
                android:src="@drawable/paprika" />

        </LinearLayout>

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:text="@string/description"
            android:textColor="#666666" />

        <Button
            android:id="@+id/_view"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/button"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="@dimen/space_small"
            android:background="@drawable/rect_rounded_light_blue"
            android:text="Button"
            android:textColor="#fff" /> 

    </LinearLayout>    
</ScrollView>

【问题讨论】:

    标签: java xml android-layout icons


    【解决方案1】:

    一些注意事项:

    • 您不需要 2 个嵌套的 LinearLayout,单个 RelativeLayout 可以处理所有视图(更少的布局 = 更好的性能)。
    • 您是否尝试过不将图像放入 ImageView 中,而是直接放入 TextView(更少的视图 = 更好的性能)?它被称为compound drawable

      android:drawableTop|Left|Right|Bottom="@drawable/your_image"

    它还可以防止 Z-Index 重叠。

    所以,我是这样组合的:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:fillViewport="true"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="@dimen/space_medium"
            >
            <TextView
                android:id="@+id/_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:layout_marginLeft="10dp"
                android:drawableRight="@drawable/paprika"
                android:drawablePadding="8dp"
                android:text="@string/title"
                android:textColor="#667"
                android:textSize="20sp"
            />
            <TextView
                android:id="@+id/description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/_title"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp"
                android:text="@string/description"
                android:textColor="#667"
            />
            <Button
                android:id="@+id/_view"
                android:layout_width="fill_parent"
                android:layout_height="@dimen/button"
                android:layout_below="@id/description"
                android:layout_marginBottom="15dp"
                android:layout_marginTop="@dimen/space_small"
                android:background="@drawable/rect_rounded_light_blue"
                android:text="Button"
                android:textColor="#fff"
            />
        </RelativeLayout>
    </ScrollView>
    

    要以编程方式设置您的复合可绘制对象(一旦您引用了 TextView),只需使用 setCompoundDrawablesWithIntrinsicBounds() 方法分配一个或多个复合图像,如下所述:http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)

    【讨论】:

    • 成功了!你真的很棒
    • 我不是很厉害...只是有点经验。 ;)
    猜你喜欢
    • 1970-01-01
    • 2012-08-17
    • 2011-06-07
    • 2021-03-29
    • 1970-01-01
    • 2012-01-03
    • 2019-02-17
    • 2011-12-31
    • 1970-01-01
    相关资源
    最近更新 更多