2010-06-11 16 views
14

मैं एक वेबसाइट से नेविगेशन लोड कर रहा हूं, जहां प्रत्येक आइटम का शीर्षक, विवरण और छवि होती है। मैं एक ही धुरी के साथ केंद्रित सभी शीर्षक और विवरण प्राप्त करना चाहता हूं।छविव्यू की अधिकतम चौड़ाई अपने माता-पिता की चौड़ाई के प्रतिशत के रूप में सेट करें

मैं सूची दृश्य में प्रत्येक आइटम के लिए निम्न लेआउट का उपयोग कर रहा हूं। यह ठीक काम करता है जब छवियां चौड़ी होती हैं जितनी लंबी होती हैं, लेकिन यदि वे कोई व्यापक नहीं हैं - पाठ छवि के नीचे जाता है।

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal"> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"> 
      <TextView 
       android:layout_height="wrap_content" 
       android:textSize="10pt" 
       android:id="@+id/title" 
       android:gravity="center_horizontal" 
       android:layout_width="fill_parent" /> 
      <TextView 
       android:layout_height="wrap_content" 
       android:id="@+id/description" 
       android:layout_width="fill_parent" 
       android:gravity="center_horizontal" /> 
     </LinearLayout> 
     <View 
      android:layout_height="fill_parent" 
      android:layout_width="fill_parent" 
      android:layout_weight="3" /> 
    </LinearLayout> 
    <ImageView 
     android:id="@+id/icon" 
     android:adjustViewBounds="true" 
     android:layout_width="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 

क्या छवि को सापेक्ष लयआउट की चौड़ाई या उससे कम के 25% पर रहने के लिए मजबूर करना संभव है?

उत्तर

15

यह एक्सएमएल में नहीं किया जा सकता है। आपको कोड में आकार निर्दिष्ट करना होगा। मुझे क्या करना होता है कि गतिविधि के onCreate() में:

[संपादित:। जोड़ा आयात, नाव अंकन]

import android.widget.LinearLayout.LayoutParams; 
... 
obj.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
            LayoutParams.WRAP_CONTENT, 
            0.25f)); 

यह उपयोग का मतलब है माता पिता की ऊंचाई की 100%, लेकिन माता-पिता की चौड़ाई का 25% ।

+0

मेरा मानना ​​है कि यह LinearLayout होना चाहिए। LayoutParams, और 0.25 काम करने के लिए 0.25f होना चाहिए। – thomas88wp

+0

@ thomas88wp जाहिर है इस कोड को LinearLayout.LayoutParams आयात करने की आवश्यकता है। इसके अलावा, 0.25 और 0.25f दशमलव बिंदु के कारण जावा में एक ही चीज़ हैं। – Emmanuel

+4

बिंदु को संतुलित करने के लिए खेद है, लेकिन जावा '0.25' में एक डबल शाब्दिक है, और '0.25 एफ' एक फ्लोट शाब्दिक है। यदि किसी पैरामीटर को फ्लोट (इस मामले में) की आवश्यकता होती है, तो यह संकलित नहीं होगा यदि आप इसे एक डबल पास करते हैं (हालांकि दूसरी तरफ काम करता है, क्योंकि फ्लोट को दोहराया जा सकता है लेकिन विपरीत नहीं)। प्रदर्शित करने के लिए, 'सार्वजनिक शून्य फू (फ्लोट बार) {foo (0.5) संकलित करने का प्रयास करें; } '- यह तब तक काम नहीं करेगा जब तक आप 'एफ' जोड़ नहीं लेते। – thomas88wp

संबंधित मुद्दे