2012-01-19 12 views
8

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

क्या मुझे ऑन मेजर विधि पर कुछ करने की ज़रूरत है?

यहाँ बातें मैं अभी कोशिश कर रहा हूँ में से एक है, दृश्य .. केंद्र के बाईं ओर है

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


    <RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="50dp" 
     android:gravity="center_horizontal"> 

     <com.application.app.ui.CustomView 
      android:id="@+id/ivCoinAnimation" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true"/> 


    </RelativeLayout> 


</FrameLayout> 
+0

क्या आप अपनी कोशिश की गई एक छोटी सी उदाहरण प्रदान कर सकते हैं? कुछ नेस्टेड लीनियरआउट्स और गुरुत्वाकर्षण विशेषता के साथ दृश्य को केंद्रित करना संभव होना चाहिए। – henrik

+0

मैंने कोशिश की चीजों में से एक का कुछ उदाहरण जोड़ा – tresnotas

उत्तर

1

आप यह मानते हुए FrameLayout की तरह एक लेआउट के अंदर एक View है, तो आप View स्थापित करेगा ' एस layout_gravity से centergravity के साथ इसे मिश्रण न करें!

इसके अलावा, यह वास्तव में मेजर() पर ओवरराइड करना आवश्यक हो सकता है। यह तय करने के लिए कि आपको इसे ओवरराइड करने की आवश्यकता है, it's documentation पढ़ें। यदि आप करते हैं, तो ध्यान रखें कि पैरामीटर View.MeasureSpec के साथ एन्कोड किए गए हैं।

-1

यहां एक उदाहरण है कि स्क्रीन पर दो बटन कैसे केंद्रित करें। यह आपके कस्टम व्यू के साथ भी काम करना चाहिए।

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

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal"> 

     <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 

    </LinearLayout> 
</LinearLayout> 
0

आप अपने कस्टम दृश्य वर्ग

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 


इसमें आप अपने कस्टम दृश्य के लिए ऊंचाई और चौड़ाई की गणना करना चाहिए में लागू करना चाहिए। आप माता-पिता लेआउट में केंद्रीय अपने कस्टम दृश्य करने की जरूरत है - सुनिश्चित करें कि:

  • कस्टम देखें ऊंचाई = माता पिता देखें ऊंचाई

और/या

  • कस्टम दृश्य चौड़ाई! ! = पैरेंट देखें चौड़ाई


उदाहरण के लिए:

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     final int height = mCircleRadius * 2; //calculated 
     final int width = getMeasuredWidth(); //parent width 

     setMeasuredDimension(width, height); 
    } 


अब आप अपने कस्टम दृश्य के लिए अगले उपयोग कर सकते हैं:

android:layout_centerHorizontal="true" 
android:layout_centerVertical="true" 

enter image description here

पी.एस. आप देखते हैं कि android:layout_centerHorizontal="true" कोई प्रभाव नहीं देता है। कारण पैरेंट चौड़ाईपर सटीक चौड़ाई की गणना के बजाय पर उपयोग कर रहा है।

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