bố cục android: Thẻ này và các thẻ con của nó có thể được thay thế bằng một <TextView /> và một kết hợp có thể vẽ được


116

Khi tôi chạy bố cục trên một tệp XML cụ thể, tôi nhận được điều này:

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

Thay đổi nào nên được thực hiện cho mã xml sau:

<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>

Đây là cách nó trông giống như trên màn hình:

nhập mô tả hình ảnh ở đây

Biểu tượng máy ảnh là biểu tượng mặc định. Nhấp vào đó sẽ cung cấp cho người dùng một tùy chọn để chọn một hình ảnh khác.


3
+1, bởi vì tình huống này trông phức tạp hơn có thể được xử lý bằng phức hợp drawable. Nếu bạn không chấp nhận câu trả lời của Romain, bạn có thể nhận được một câu trả lời chuyên sâu hơn.
AlbeyAmakiir

Câu trả lời:


153

Để mở rộng câu trả lời của Romain Guy, đây là một ví dụ.

Trước:

<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>

Sau:

<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" />

45
Xấu. Nhưng điều gì sẽ xảy ra nếu tôi cần đặt các thuộc tính trên drawable, chẳng hạn như xác định nó thành 60dip x 60dip?
Kyle Clegg

4
Này, nhìn kìa, tất cả những gì tôi phải làm là tìm kiếm. stackoverflow.com/a/6671544/1224741
QED

@KyleClegg, Có thể có một thư viện tùy chỉnh. Xem câu trả lời của tôi - stackoverflow.com/a/41347470/2308720
Oleksandr

102

Hợp nhất TextViewImageViewthành một, bằng cách sử dụng setCompoundDrawable*()các phương pháp của TextView hoặc sử dụng android:drawableLeft.


30
Bạn có thể chỉ cho tôi một ví dụ minh họa cách làm điều đó được không? Tôi không chắc làm thế nào tất cả các thuộc tính trong ImageView có thể được điều chỉnh trong android: drawableLeft. Cảm ơn bạn
pdilip

1
Còn các thuộc tính ImageViews như scaleType thì sao?
neteinstein

2
Làm cách nào để tăng khoảng cách giữa hình ảnh và văn bản?
TharakaNirmana

2
setCompoundDrawable * () không hoạt động, hãy sử dụng setCompoundDrawablesWithIntrinsicBounds () để thay thế
Kimo_do

1
@Kimo_do, setCompoundDrawable()sẽ không chăm sóc khả năng vẽ của bạn như nó vốn có, bạn phải gọi setBoundsnó (xem API ). Để thiết lập nó, bạn có thể lấy các giới hạn trước đó bằng cách sử dụng TextView.getCompoundDrawables(), truy cập vào chỉ mục có thể vẽ chính xác và cuối cùng là gọi getBounds().
Avinash R

19

Tôi nghĩ rằng tôi cũng sẽ cố gắng kiếm thêm một số puntos cho việc này: bạn có thể thêm phần đệm giữa hình ảnh và văn bản bằng cách sử dụng android:drawablePadding. https://stackoverflow.com/a/6671544/1224741


3

Đôi khi có thể thay thế ImageView(hoặc nhiều) và TextViewbằng một TextView(các) phức hợp có thể kéo được. KHÔNG có nhiều tham số có thể được áp dụng cho kết hợp có thể vẽ bằng API gốc và thư viện TextViewRichDrawable này , nhưng nếu bạn có thể quản lý một TextView thay vì sử dụng LinearLayout, bạn chắc chắn nên sử dụng nó .

Danh sách các thuộc tính và tham số có thể được áp dụng cho các phần mềm có thể kéo được:

Kích thước: ( CÓ, thực sự ):

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

Thậm chí đặt tài nguyên vectơ là có thể vẽ được:

<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 sử dụng liên kết API gốc android:drawablePadding->

Đây là một ví dụ:

textView nhiều định dạng có thể vẽ


2

Một LinearLayouttrong đó có một ImageViewTextViewcó thể bị xử lý một cách hiệu quả hơn như là một hợp chất có thể vẽ được (một đơn TextView, sử dụng drawableTop, drawableLeft, drawableRight và / hoặc drawableBottomcác thuộc tính để vẽ một hoặc nhiều hình ảnh tiếp giáp với văn bản).

Nếu hai widget được bù trừ với nhau bằng các lề, thì điều này có thể được thay thế bằng một drawablePaddingthuộc tính.

Có một cập nhật nhanh xơ vải để thực hiện chuyển đổi này trong plugin Eclipse.

Từ: Tài liệu API chính thức của Android!


2

Thêm tools:ignore="UseCompoundDrawables"vào <LinearLayout>.


1

Khi tôi làm theo đoạn mã trên, văn bản bên trong TextView không được đặt đúng cách. Bạn cần đặt trọng lực của nó ở tâm | bắt đầu đạt được những gì được thể hiện trong câu hỏi đã hỏi.

Chế độ xem văn bản trông như thế này:

   <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>

0

Nếu bạn không muốn thay đổi ImageView và TextView, bạn có thể thay đổi phiên bản trong AndroidManifest.xml như sau:

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

Nếu phiên bản của bạn là android: targetSdkVersion = "17", hãy đổi thành "18".

Hy vọng điều này sẽ khắc phục. Tôi đã làm nó và làm đúng


-2

Một cách tiếp cận khác là nhúng ViewImage vào một LinearLayout khác (cho phép xử lý nó với một mình 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" />


Bây giờ những gì sẽ là lợi thế của điều này? Và tại sao bạn đặt không gian tên hai lần?
ygesher

-5
    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>
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.