android layout:这个标签及其子标签可以被一个 <TextView/> 和一个复合可绘制对象替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3214424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:10:40  来源:igfitidea点击:

android layout: This tag and its children can be replaced by one <TextView/> and a compound drawable

androidlayout

提问by pdilip

When I run layout on a specific XML file, I get this:

当我在特定的 XML 文件上运行布局时,我得到以下信息:

 This tag and its children can be replaced by one <TextView/> 
and a compound drawable

What change should be done for the following xml code:

下面的xml代码应该做哪些改动:

<LinearLayout android:id="@+id/name_layout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:background="@drawable/grouplist_single_left_grey_area" >
                <ImageView android:id="@+id/photo_image"
                    android:layout_width="@dimen/thumbnail_width"
                    android:layout_height="@dimen/thumbnail_height"
                    android:paddingBottom="5dip"
                    android:paddingTop="5dip"
                    android:paddingRight="5dip"
                    android:paddingLeft="5dip"
                    android:layout_marginRight="5dip"
                    android:clickable="true"
                    android:focusable="true"
                    android:scaleType="fitCenter"
                    android:src="@*android:drawable/nopicture_thumbnail"
                    android:background="@drawable/photo_highlight" />
                <TextView android:id="@+id/name"
                    android:paddingLeft="5dip"
                    android:layout_weight="1"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:textAppearance="?android:attr/textAppearanceLarge" />
            </LinearLayout>

This is how it looks like on the screen:

这是它在屏幕上的样子:

enter image description here

在此处输入图片说明

The camera icon is the default. Clicking on that will give the user an option to choose another image.

相机图标是默认设置。点击它会给用户一个选择另一个图像的选项。

回答by NPike

To expand on Romain Guy's answer, here is an example.

为了扩展 Romain Guy 的回答,这里有一个例子。

Before:

前:

<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:layout_marginTop="10dp"  
android:padding="5dp" >

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="My Compound Button" />

<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/my_drawable" />
</LinearLayout>

After:

后:

<TextView  
    android:layout_marginTop="10dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="My Compound Button" 
    android:drawableRight="@drawable/my_drawable" android:padding="5dp" />

回答by Romain Guy

Merge the TextViewand the ImageViewinto one, by using TextView's setCompoundDrawable*()methods, or using android:drawableLeft.

合并TextViewImageView成一体,通过使用TextView的的setCompoundDrawable*()方法,或者使用android:drawableLeft

回答by QED

Thought I would try to get some extra puntos for this as well: you can add padding between the image and the text using android:drawablePadding. https://stackoverflow.com/a/6671544/1224741

我想我也会尝试为此获得一些额外的 puntos:您可以使用android:drawablePadding. https://stackoverflow.com/a/6671544/1224741

回答by Oleksandr

Sometimes it is possible to replace ImageView(or multiple) and TextViewwith one TextViewwith compound drawable(s). There are NOT many parameters which can be applied to compound drawable using native API and this TextViewRichDrawable library, but if you can manage one TextView instead of using LinearLayout you should definitely use it.

有时可以用复合可绘制对象替换ImageView(或多个)和TextView一个TextView。使用本机 API 和这个TextViewRichDrawable 库可以应用于复合可绘制的参数并不多,但是如果您可以管理一个 TextView 而不是使用 LinearLayout,您绝对应该使用它

The list of attributes and parameters which can be applied to compound drawables:

可应用于复合绘图的属性和参数列表:

Size: (YES, really):

尺寸:(是的,真的):

<com.tolstykh.textviewrichdrawable.TextViewRichDrawable
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Some text"
    app:compoundDrawableHeight="24dp"
    app:compoundDrawableWidth="24dp"/>

Even set vector resourceas drawable:

甚至将矢量资源设置为可绘制:

<com.tolstykh.textviewrichdrawable.TextViewRichDrawable
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Some text"
    app:drawableTopVector="@drawable/some_vector_drawble"
    app:drawableEndVector="@drawable/another_vector_drawable" />

Drawable's Padding using native API android:drawablePadding-> link

使用原生 API 的 Drawable Padding android:drawablePadding->链接

Here is an example:

下面是一个例子:

textView rich drawable

textView 丰富的 drawable

回答by ilikyar

A LinearLayoutwhich contains an ImageViewand a TextViewcan be more efficiently handled as a compound drawable (a single TextView, using the drawableTop, drawableLeft, drawableRightand/or drawableBottomattributes to draw one or more images adjacent to the text).

LinearLayout其含有ImageViewTextView可以作为一个化合物绘制被更有效地处理(单 TextView,使用drawableTopdrawableLeftdrawableRight和/或drawableBottom属性来绘制邻近于文本的一个或多个图像)。

If the two widgets are offset from each other with margins, this can be replaced with a drawablePaddingattribute.

如果两个小部件通过边距彼此偏移,则可以用drawablePadding属性替换。

There's a lint quickfix to perform this conversion in the Eclipse plugin.

有一个 lint quickfix 可以在 Eclipse 插件中执行此转换。

From: Android Official API docs!

来自:Android 官方 API 文档!

回答by CoolMind

Add tools:ignore="UseCompoundDrawables"to <LinearLayout>.

添加tools:ignore="UseCompoundDrawables"<LinearLayout>.

回答by Vikas

When I followed the code above, text inside the TextView doesn't set properly. You need to set its gravity to center|start to achieve what shown in the asked question.

当我按照上面的代码进行操作时,TextView 中的文本设置不正确。您需要将其重力设置为 center|start 以实现所问问题中显示的内容。

The textview looks like this:

文本视图如下所示:

   <TextView
        android:id="@+id/export_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawableLeft="@drawable/up_arrow"
        android:drawableStart="@drawable/up_arrow"
        android:gravity="center|start"
        android:text="....."
        android:textSize="@dimen/font_size15" >
    </TextView>

回答by Dunken_sai

If you don't want to change the ImageView and TextView, you can change the version in the AndroidManifest.xml as:

如果您不想更改 ImageView 和 TextView,则可以将 AndroidManifest.xml 中的版本更改为:

    <uses-sdk`
    android:minSdkVersion="8"
    android:targetSdkVersion="18" 
    />

If your version is android:targetSdkVersion="17" change it s "18".

如果您的版本是 android:targetSdkVersion="17",请将其更改为“18”。

Hope this will rectify. I did it and got it right

希望这会得到纠正。我做到了并且做对了

回答by Harvey Triana

Another approach is embed the ViewImage into another LinearLayout (allow handle it with alone id):

另一种方法是将 ViewImage 嵌入到另一个 LinearLayout 中(允许使用单独的 id 处理它):

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/blue_3"
            android:layout_gravity="center_horizontal"
            android:paddingTop="16dp" />
    </LinearLayout>
    <TextView 
        android:id="@+id/tvPrompt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:paddingTop="16dp"
        android:text="@string/xy" />

回答by andro_stackoverflow

    This tag and its children can be replaced by one <TextView/> and a compound drawable



    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:contentDescription="."
        android:padding="3dp" 
        android:src="@drawable/tab_home_btn">
    </ImageView>

    <TextView
        android:id="@+id/textview"       
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="首页"
        android:textSize="10sp"
        android:textColor="#ffffff">
    </TextView>

</LinearLayout>