Android लेआउट: इस टैग और इसके बच्चों को एक <TextView /> और एक कंपाउंड ड्रिबल द्वारा प्रतिस्थापित किया जा सकता है


116

जब मैं किसी विशिष्ट XML फ़ाइल पर लेआउट चलाता हूं, तो मुझे यह मिलता है:

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

निम्नलिखित 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>

यह स्क्रीन पर कैसा दिखता है:

यहां छवि विवरण दर्ज करें

कैमरा आइकन डिफ़ॉल्ट है। उस पर क्लिक करने से उपयोगकर्ता को दूसरी छवि चुनने का विकल्प मिलेगा।


3
+1, क्योंकि यह स्थिति अधिक जटिल दिखती है, जिसे कंपाउंड ड्रॉबल द्वारा नियंत्रित किया जा सकता है। यदि आपने रोमेन के उत्तर को स्वीकार नहीं किया है, तो आपको अधिक गहराई से उत्तर मिल सकता है।
एल्बेअमकीर

जवाबों:


153

रोमेन गाइ के जवाब पर विस्तार करने के लिए, यहां एक उदाहरण है।

इससे पहले:

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

उपरांत:

<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
निफ्टी। लेकिन क्या होगा अगर मुझे ड्रॉ करने योग्य पर गुण सेट करने की आवश्यकता है, जैसे कि इसे 60dip x 60dip में परिभाषित करें?
काइल क्लेग

4
अरे वो देखो, मुझे बस इतना ही करना था। stackoverflow.com/a/6671544/1224741
QED

@KyleClegg, यह एक कस्टम लाइब्रेरी के साथ संभव है। - मेरा उत्तर देखें stackoverflow.com/a/41347470/2308720
ऑलेक्ज़ेंडर

102

TextView के तरीकों का उपयोग करके, या का उपयोग करके TextViewऔर एक ImageViewको मर्ज करें ।setCompoundDrawable*()android:drawableLeft


30
क्या आप मुझे उदाहरण के लिए बता सकते हैं कि यह कैसे किया जा सकता है? मुझे यकीन नहीं है कि ImageView में सभी विशेषताओं को एंड्रॉइड में कैसे समायोजित किया जा सकता है: drawableLeft। धन्यवाद
pdilip

1
ImageViews के बारे में क्या पैमाने की तरह विशेषताएँ?
neteinstein

2
मैं छवि और पाठ के बीच की खाई को कैसे बढ़ाऊं?
थरकनरिमाना

2
setCompoundDrawable * () काम नहीं कर रहा है, इसके बजाय setCompoundDrawablesWithIntrinsicBounds () का उपयोग करें
Kimo_do

1
@Kimo_do, आपके ड्रॉएबल setCompoundDrawable()का ख्याल नहीं रखेगा क्योंकि यह है, आपको इस पर कॉल setBoundsकरना होगा ( एपीआई देखें )। इसे स्थापित करने के लिए, आप TextView.getCompoundDrawables()सही ड्रॉबल इंडेक्स तक पहुंचने और अंत में कॉलिंग का उपयोग करके पिछले सीमा प्राप्त कर सकते हैं getBounds()
अविनाश आर

19

सोचा कि मैं इसके लिए कुछ अतिरिक्त पंटोस प्राप्त करने की कोशिश करूंगा: आप छवि और पाठ के बीच पैडिंग जोड़ सकते हैं android:drawablePaddinghttps://stackoverflow.com/a/6671544/1224741


3

कभी-कभी यह ImageView(या एकाधिक) को बदलने और यौगिक ड्रॉबल (एस) के TextViewसाथ एक के लिए संभव है TextView। ऐसे कई पैरामीटर नहीं हैं, जिन्हें मूल API और इस TextViewRichDrawable लाइब्रेरी का उपयोग करके कंपैक्ट ड्रायबल पर लागू किया जा सकता है , लेकिन यदि आप LinearLayout का उपयोग करने के बजाय एक TextView का प्रबंधन कर सकते हैं , तो आपको इसका उपयोग जरूर करना चाहिए

उन विशेषताओं और मापदंडों की सूची, जिन्हें कंपाउंड ड्रॉबल्स पर लागू किया जा सकता है:

आकार: ( हाँ, वास्तव में ):

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

यहां तक ​​कि सदिश संसाधन को भी खाने योग्य के रूप में सेट करें :

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

मूल एपीआई android:drawablePadding-> लिंक का उपयोग करके ड्रॉएबल की पैडिंग

यहाँ एक उदाहरण है:

अमीर देखने योग्य


2

एक LinearLayoutजिसमें एक ImageViewऔर एक TextViewऔर अधिक कुशलता से एक यौगिक drawable के रूप में संभाला जा सकता है (एक TextViewका उपयोग करते हुए drawableTop, drawableLeft, drawableRight और / या drawableBottomएक या अधिक छवियों पाठ के निकट आकर्षित करने के लिए जिम्मेदार बताते हैं)।

यदि दो विगेट्स मार्जिन से एक-दूसरे से ऑफसेट होते हैं, तो इसे एक drawablePaddingविशेषता से बदला जा सकता है ।

एक्लिप्स प्लगइन में इस रूपांतरण को करने के लिए एक लिंट क्विकफिक्स है।

से: Android आधिकारिक एपीआई डॉक्स!



1

जब मैंने उपरोक्त कोड का पालन किया, तो TextView के अंदर का पाठ ठीक से सेट नहीं होता है। आपको इसके गुरुत्वाकर्षण को केंद्र में स्थापित करने की आवश्यकता है। पूछे गए प्रश्न में जो दिखाया गया है उसे प्राप्त करना शुरू करें।

टेक्स्टव्यू इस तरह दिखता है:

   <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

यदि आप ImageView और TextView को बदलना नहीं चाहते हैं, तो आप AndroidManifest.xml में संस्करण बदल सकते हैं:

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

यदि आपका संस्करण android है: targetSdkVersion = "17" इसे बदल दें "18"।

उम्मीद है कि इससे सुधार होगा। मैंने इसे किया और सही किया


-2

एक अन्य दृष्टिकोण ViewImage को एक अन्य रैखिकलेयूट में एम्बेड करें (इसे अकेले आईडी के साथ संभालने की अनुमति दें):

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


अब इससे क्या फायदा होगा? और आप दो बार नाम स्थान क्यों सेट कर रहे हैं?
यगेशर

-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>
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.