2013-09-26 9 views
9

में ठीक से काम नहीं कर रहा है मेरे कोड में क्या गड़बड़ है, मैं अपने टेक्स्ट व्यू को "invalid" नामित करने की कोशिश कर रहा हूं, अलग-अलग स्थानों (बाएं, दाएं, केंद्र) में, लेकिन गुरुत्वाकर्षण (बाएं, दाएं, केंद्र) ने ' टी काम नहीं! enter image description hereटेक्स्टव्यू गुरुत्वाकर्षण एंड्रॉइड

मेरे text.xml है

<?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:padding="20dp" > 

    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/etext" 
     android:hint="@string/comment" 
     android:inputType="textPassword"/> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="100"> 
     <Button 
      android:id="@+id/button1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/button" 
      android:layout_weight="25"/> 

     <ToggleButton 
      android:id="@+id/toggleButton1" 
      android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:text="ToggleButton" 
      android:layout_weight="75" 
      android:checked="true" 
      android:paddingLeft="15dp"/> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/invalid" 
     android:layout_gravity="center" 
     android:gravity="center" /> 
</LinearLayout> 

मेरे TextPlay.java

public class TextPlay extends Activity { 
    Button button; 
    ToggleButton tbutton; 
    TextView tview; 
    EditText et; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.text); 
     button = (Button) findViewById(R.id.button1); 
     tbutton = (ToggleButton) findViewById(R.id.toggleButton1); 
     tview = (TextView) findViewById(R.id.textView1); 
     et = (EditText) findViewById(R.id.etext); 
     tbutton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if (tbutton.isChecked()) { 
        et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
       } else { 
        et.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); 
       } 
      } 
     }); 
     button.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       String input = et.getText().toString(); 
       System.out.println(input); 
       if (input.contentEquals("left")) { 
        tview.setGravity(Gravity.LEFT); 
       } else if (input.contentEquals("right")) { 
        System.out.println("inside right"); 
        tview.setGravity(Gravity.RIGHT); 
       } else if (input.contentEquals("right")) { 
        tview.setGravity(Gravity.CENTER); 
       } 
      } 
     }); 
    } 
} 
+0

निकालने एंड्रॉयड की तरह कुछ न करना पड़े: layout_gravity = "केन्द्र" एंड्रॉयड: गुरुत्वाकर्षण "केन्द्र" = से @ + id/textView1 के लिए अपना लेआउट और प्रयास करें ... – Vamshi

+0

'टेक्स्ट व्यू' की चौड़ाई को 'match_parent' में बदलें या' एंड्रॉइड: लेआउट_ग्रैविटी 'को स्थिरता में बदलें। –

+0

इस प्रश्न को भी देखें: [link] (http://stackoverflow.com/questions/3482742/android-gravity-and-layout-gravity) – andysando

उत्तर

20

आप इस पाठ की "wrap_content" चौड़ाई देखने सेट किया गया है इसका मतलब है, क्या कभी पाठ है, दृश्य पाठ का आकार लेते हैं। समय की

<TextView 
android:id="@+id/textView1" 
android:layout_width="match_parent" <= change this 
android:layout_height="wrap_content" 
android:text="@string/invalid" 
android:gravity="center" <= then this gravity will be taken into account 
/> 
26

99%, not working properly == not used properly:

और LinearLayout में, डिफ़ॉल्ट गुरुत्व (यहां इस्तेमाल किया) 'केंद्र'

आप इस प्रयास करना चाहिए है।

आप gravity और layout_gravity गलत कर रहे हैं।

gravity रास्ता पाठ TextView में ही संरेखित जाएगा। TextViewwrap_content में यह कुछ भी नहीं करता है, क्योंकि TextView बिल्कुल टेक्स्ट का आकार है।

layout_gravity खड़ी LinearLayout

+0

@ हार्मन 'गुरुत्वाकर्षण' केवल 'टेक्स्ट व्यू' के लिए परिभाषित एक विशेषता है। – njzk2

1

आप अपने TextView चौड़ाई wrap_content दे दिया है में अपने मामले में, जिस तरह से TextView अपनी मूल में ही संरेखित जाएगा, और कहा कि समस्या है, नीचे दिए गए कोड की जाँच करें और करने के लिए इसे बदलना आपके कोड।

<TextView 
android:id="@+id/txtInvalid" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:text="@string/invalid" 
android:gravity="center" 
/> 
0

textView1 के लिए android:layout_width="fill_parent" सेट

0

सुनिश्चित करें कि आप के आसपास फांसी

app:layout_box="left|top" 
संबंधित मुद्दे