15

के अंदर प्रतिशत रिटेलिवेटआउट मैंने ScrollView का उपयोग करके एक लेआउट बनाया है जिसमें PercentRelativeLayout है जो उसके बच्चे के रूप में है। यह लॉलीपॉप और पुराने उपकरणों पर काम नहीं करता है लेकिन मार्शमलो डिवाइस पर ठीक काम करता है। नीचे दिए गए कोड की जाँच करें:स्क्रॉलव्यू

<ScrollView 
    android:id="@+id/scrollView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <android.support.percent.PercentRelativeLayout 
     android:id="@+id/scrollContent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_heightPercent="100%" 
     app:layout_widthPercent="50%"> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/holo_red_dark" 
      android:text="kkjknadko" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textView1" 
      android:text="Abcaad" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview2" 
      android:background="@android:color/holo_red_dark" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview3" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview4" 
      android:background="@android:color/holo_red_dark" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview5" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview6" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview8" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview7" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

    </android.support.percent.PercentRelativeLayout> 
</ScrollView> 

और यह भी मैं android:fillViewport="true", यह लॉलीपॉप में कुछ भी और पुराने Android संस्करण प्रदर्शित नहीं करता है।

दुर्भाग्यवश, प्रतिशत लेआउट एम से पहले ScrollView के साथ काम नहीं करेगा। इसका कारण यह है कि वे मापने के चरण में दिए गए आकार संकेत पर निर्भर करते हैं। एम से पहले अधिकांश लेआउट अनुपयुक्त माप spec भेजते समय आकार संकेत 0 प्रदान करेगा।

आपको लगता है कि ठीक करने के लिए ScrollView और अधिभावी measureChild और measureChildWithMargins की अपनी खुद की उपवर्ग (सौभाग्य से दोनों सुरक्षित हैं) बनाने के आकार संकेत प्रदान करने के लिए कोशिश कर सकते हैं।

स्रोत - plus.google.com।

क्या कोई इसे काम करने के लिए कस्टम ScrollView बनाने में मेरी सहायता कर सकता है?

+0

कोशिश NestScrollView बजाय scrollview उपयोग करने के लिए। –

+0

मैंने कोशिश की है लेकिन यह –

उत्तर

0

जैसा सवाल मैं समझता हूं वह यह है: मैं चाहता हूं कि ये टेक्स्ट व्यू माता-पिता के दृश्य की 50% चौड़ाई हो।

रास्ता है कि मेरे लिए काम करता है:

<Space 
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/space" /> 

फिर उस को देखें (रों) पंक्ति। या तो रिलेटिवलाउट या टेक्स्ट व्यू। रिलेवेटिवआउट के प्रतिशत से आप नियंत्रण खो देते हैं (यदि आपने कभी किया था)। मुझे चीजों के साथ जाना पड़ता है जैसे:

android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 

क्या इससे मदद मिलती है?

+0

काम नहीं करता है, असल में मुझे स्क्रॉलव्यू के अंदर काम करने के लिए प्रतिशत रिटेलिवेट करना है। मैंने परिदृश्य को दिखाने के लिए उदाहरण दिया है जो सटीक नहीं है जिसका मैं उपयोग कर रहा हूं –

7

एक कस्टम scrollview बनाएँ और Measured Height और Width सेट के रूप में आप अपने एक्सएमएल में उदाहरण के लिए

CustomScrollView

public class CustomScrollView extends ScrollView { 


    public CustomScrollView(Context context) { 
     super(context); 
    } 

    public CustomScrollView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     int size = 0; 
     int width = getMeasuredWidth(); 
     int height = getMeasuredHeight(); 

     if (width > height) { 
      size = height; 
     } else { 
      size = width; 
     } 
     setMeasuredDimension(size, size); 
    } 
} 

उपयोग customscrollview यह करना चाहते हैं।

<yourscrollviewclasspath.CustomScrollView   // here you have scrollview path like com.yourpackage.folder_where_your_scrollview_lies 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" 
     > 
</yourscrollviewclasspath.CustomScrollView > 

enter image description here आशा है कि यह मदद मिलेगी।

0

मैं आप बाधा लेआउट के बजाय प्रतिशत सापेक्ष लेआउट का इस्तेमाल किया जा सकता था लेकिन वहाँ स्क्रॉल कि के साथ समस्या यह भी लगता है एंड्रॉयड कश्मीर और एंड्रॉयड एल

साथ एक ही समस्या का सामना करना पड़ा। अन्य उत्तरों में सुझाए गए कस्टम स्क्रॉल व्यू भी मेरे लिए सहायक नहीं थे।

हालांकि एक तरीका है।

अपने प्रतिशत रिश्तेदार लेआउट को रिश्तेदार लेआउट में बदलें और अपने डिवाइस की ऊंचाई के अनुसार प्रोग्रामेटिक रूप से दृश्यों की ऊंचाई सेट करें।

सापेक्ष लेआउट के साथ स्क्रॉल करने में कोई समस्या नहीं है। और आप TextViews के बीच मार्जिन के लिए 'स्पेस' व्यू का उपयोग कर सकते हैं। यह एक बहुत ही सुरुचिपूर्ण समाधान नहीं है लेकिन मेरे लिए यह काम करता है।

जावा कोड:

int height = getWindowManager().getDefaultDisplay().getHeight(); 
int width = getWindowManager().getDefaultDisplay().getWidth(); 

// to set height to each textview to 10% of screen 
textView1.getLayoutParams().height = (int) (height * 0.10); 
textView2.getLayoutParams().height = (int) (height * 0.10); 
textView3.getLayoutParams().height = (int) (height * 0.10); 
textView4.getLayoutParams().height = (int) (height * 0.10); 
textView5.getLayoutParams().height = (int) (height * 0.10); 
textView6.getLayoutParams().height = (int) (height * 0.10); 

// to set width to each textview to 50% of screen 
textView1.getLayoutParams().width = (int) (width * 0.50); 
textView2.getLayoutParams().width = (int) (width * 0.50); 
textView3.getLayoutParams().width = (int) (width * 0.50); 
textView4.getLayoutParams().width = (int) (width * 0.50); 
textView5.getLayoutParams().width = (int) (width * 0.50); 
textView6.getLayoutParams().width = (int) (width * 0.50); 

// for margin between textviews 
space1.getLayoutParams().height = (int) (height * 0.10); 
space2.getLayoutParams().height = (int) (height * 0.10); 
space3.getLayoutParams().height = (int) (height * 0.10); 
space4.getLayoutParams().height = (int) (height * 0.10); 
space5.getLayoutParams().height = (int) (height * 0.10); 

एक्सएमएल कोड:

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/scrollView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fillViewport="true"> 

<RelativeLayout 
    android:id="@+id/scrollContent" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_red_dark" 
     android:text="kkjknadko" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space1" 
     android:layout_below="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space1" 
     android:text="Abcaad" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space2" 
     android:layout_below="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space2" 
     android:background="@android:color/holo_red_dark" 
     android:text="Abcd" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space3" 
     android:layout_below="@+id/textView3" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space3" 
     android:text="Abcd" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space4" 
     android:layout_below="@+id/textView4" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

    <TextView 
     android:id="@+id/textview5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space4" 
     android:background="@android:color/holo_red_dark" 
     android:text="Abcd" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space5" 
     android:layout_below="@+id/textview5" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

    <TextView 
     android:id="@+id/textview6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space5" 
     android:text="Abcd" 
     android:textColor="@android:color/black"/> 

</RelativeLayout>