2012-10-08 14 views
6

संपादित करें: मुझे निश्चित मार्जिन के संबंध में पहले से ही दो व्यापक उत्तर प्राप्त हुए हैं। जबकि मैंने वजन मार्जिन के बजाय निश्चित मार्जिन का उपयोग करने का फैसला किया है, मूल प्रश्न खुला रहता है।केंद्र एक लीनियरआउट और "प्रतिशत" मार्जिन

मैं Android में निम्नलिखित डिजाइन प्राप्त करने के लिए कोशिश कर रहा हूँ:

Desired Layout

एक सामान के ऊर्ध्वाधर सूची केंद्रित (TextViews, EditViews आदि) के रूप में छोड़ दिया जो क्षैतिज स्थान का लगभग 10% छोड़ देता है नि: शुल्क/पृष्ठभूमि के साथ सही मार्जिन।

मैं क्या करने की कोशिश की और काम नहीं किया/आंशिक रूप से काम किया:

  • LinearLayout, खड़ी, उच्च-स्तरीय लेआउट के रूप में। यदि गुरुत्वाकर्षण "केंद्रित" पर सेट है, तो पृष्ठभूमि लेआउट के आकार तक ही सीमित है। साथ ही, इस तरीके से एक प्रतिशत प्रतिशत मार्जिन (चौड़ाई) कैसे निर्धारित करता है?
  • रिलेवेटिवआउट पर लीनियरआउटआउट: पृष्ठभूमि कार्य, क्षैतिज केंद्रित काम, वजन मौजूद नहीं है।
  • रैखिक स्तर पर लाइनरलायआउट: पृष्ठभूमि कार्य, वजन कार्य, क्षैतिज केंद्रिंग सभी उपलब्ध स्थान को दाईं ओर धक्का देती है।

(पिछले दो मामलों में, मेरी ग्रहण भी शिकायत है कि लेआउट में से एक अनावश्यक है।)

है कि यह एक सिद्धांत से संबंधित प्रश्न के कुछ और अधिक है माना जाता रहा है मैं कोड पोस्ट नहीं किया है,। इसे पूरा करने का सबसे अच्छा तरीका क्या होगा?

धन्यवाद।

एक्सएमएल परीक्षण मामले की पिछले एक करने के लिए इसी:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:baselineAligned="false" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1.0" 
    android:weightSum="1.0" 
    android:background="#013c57" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/linearLayout1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.9" 
     android:layout_gravity="center" 
     android:orientation="vertical" > 

     <!-- Stuff --> 
    </LinearLayout> 
</LinearLayout> 
+0

आप हमारे साथ अपने xml फ़ाइल का हिस्सा बता सकते हैं:

यहाँ उदाहरण है? –

उत्तर

2

यहाँ लेआउट के इस प्रकार बनाने के लिए सबसे आसान एक्सएमएल कोड की जांच कर रहा है यह

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

    <EditText android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp"/> 

    <EditText android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp"/> 

    <EditText android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp"/> 

    <EditText android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp"/> 
</LinearLayout> 
+0

हालांकि यह वास्तव में पृष्ठभूमि को संरक्षित करता है और संभावित रूप से अनावश्यक लेआउट को समाप्त करता है, यह बाएं और दाएं मार्जिन को कैसे पूरा करता है? – TotoTitus

+0

बस इन दो कथन को जोड़कर और एंड्रॉइड को हटाकर: layout_margin = "10dp", (** android: layout_marginLeft = "10dp" और एंड्रॉइड: layout_marginRight = "10dp" **) – Pratik

+0

मैं समझता हूं, आपका उत्तर उपयोगी है, लेकिन यह procent, या वजन आधारित मार्जिन प्रदान नहीं करता है। (जो वैसे भी एक अच्छा विचार नहीं हो सकता है; निश्चित मार्जिन शायद बेहतर हैं, लेकिन यह मूल प्रश्न के लिए प्रासंगिक है)। – TotoTitus

0

मुझे लगता है कि सापेक्ष लेआउट एक बेहतर है पसंद!

इस सूत्र को पढ़ने के लिए प्रयास करें:

Percentage width in a RelativeLayout

मुझे आशा है कि यह आप में मदद मिलेगी!

0
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#000" 
    android:gravity="center"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:background="#000" 
     android:layout_gravity="center" 
     android:gravity="center"> 

     <EditText android:layout_width="fill_parent" 
      android:margin_left="10dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

     <EditText android:layout_width="fill_parent" 
      android:margin_left="10dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

     <EditText android:layout_width="fill_parent" 
      android:margin_left="10dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

     <EditText android:layout_width="fill_parent" 
      android:margin_left="10dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

    </LinearLayout> 

</LinearLayout> 

इस

+0

यह समाधान प्रतििक के समान है। यह मार्जिन पूरा करता है, लेकिन प्रतिशत के रूप में नहीं। – TotoTitus

+0

वहां परिवर्तन के रूप में यदि आप इसे चेक करते हैं – Syn3sthete

1

आप compile 'com.android.support:percent:24.2.1'

अब आप PercentFrameLayout या PercentRelativeLayout उपयोग कर सकते हैं का उपयोग कर सकते प्रयास करें। वे दोनों में निम्नलिखित विशेषताएं होनी:

layout_widthPercent 
layout_heightPercent 
layout_marginPercent 
layout_marginLeftPercent 
layout_marginTopPercent 
layout_marginRightPercent 
layout_marginBottomPercent 
layout_marginStartPercent 
layout_marginEndPercent 



<?xml version="1.0" encoding="utf-8"?> 
<android.support.percent.PercentRelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/txt1" 
     android:background="#ff7acfff" 
     android:text="50% - 50% margin 25" 
     android:textColor="@android:color/white" 
     app:layout_marginTopPercent="10%" 
     app:layout_marginLeftPercent="10%" 
     app:layout_heightPercent="10%" 
     app:layout_widthPercent="80%" /> 

    <TextView 
     android:layout_below="@id/txt1" 
     android:id="@+id/txt2" 
     android:background="#ff7acfff" 
     android:text="50% - 50% margin 25" 
     android:textColor="@android:color/white" 
     app:layout_marginTopPercent="10%" 
     app:layout_marginLeftPercent="10%" 
     app:layout_heightPercent="10%" 
     app:layout_widthPercent="80%"/> 

    <TextView 
     android:layout_below="@id/txt2" 
     android:id="@+id/txt3" 
     android:background="#ff7acfff" 
     android:text="50% - 50% margin 25" 
     android:textColor="@android:color/white" 
     app:layout_marginTopPercent="10%" 
     app:layout_marginLeftPercent="10%" 
     app:layout_heightPercent="10%" 
     app:layout_widthPercent="80%"/> 

    <TextView 
     android:layout_below="@id/txt3" 
     android:id="@+id/txt4" 
     android:background="#ff7acfff" 
     android:text="50% - 50% margin 25" 
     android:textColor="@android:color/white" 
     app:layout_marginTopPercent="10%" 
     app:layout_marginLeftPercent="10%" 
     app:layout_heightPercent="10%" 
     app:layout_widthPercent="80%"/> 


</android.support.percent.PercentRelativeLayout> 
0

ConstraintLayout की शुरूआत के साथ, आप दिशानिर्देशों द्वारा उन्हें रोके द्वारा अपने विचारों के लिए प्रतिशत मार्जिन सेट कर सकते हैं।

Layout Editor

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     app:layout_constraintBottom_toTopOf="@+id/editText2" 
     app:layout_constraintEnd_toStartOf="@+id/endGuideline" 
     app:layout_constraintStart_toStartOf="@+id/startGuideline" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_chainStyle="packed" /> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     app:layout_constraintBottom_toTopOf="@+id/editText3" 
     app:layout_constraintEnd_toStartOf="@+id/endGuideline" 
     app:layout_constraintStart_toStartOf="@+id/startGuideline" 
     app:layout_constraintTop_toBottomOf="@+id/editText1" /> 

    <EditText 
     android:id="@+id/editText3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     app:layout_constraintBottom_toTopOf="@+id/editText4" 
     app:layout_constraintEnd_toStartOf="@+id/endGuideline" 
     app:layout_constraintStart_toStartOf="@+id/startGuideline" 
     app:layout_constraintTop_toBottomOf="@+id/editText2" /> 

    <EditText 
     android:id="@+id/editText4" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintEnd_toStartOf="@+id/endGuideline" 
     app:layout_constraintStart_toStartOf="@+id/startGuideline" 
     app:layout_constraintTop_toBottomOf="@+id/editText3" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/startGuideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.1" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/endGuideline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.9" /> 

</android.support.constraint.ConstraintLayout> 
संबंधित मुद्दे