2013-09-26 21 views
9

Co jest nie tak z moim kodem, próbuję wyświetlić mój TextView o nazwie "invalid", w różnych lokalizacjach (lewo, prawo, centrum), ale grawitacja (lewo, prawo, centrum) wygrał " t działa! enter image description hereTextview Gravity nie działa poprawnie w Androidzie

Moja text.xml jest

<?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> 

Moja TextPlay.java jest

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

remove Androidzie: layout_gravity = "center" android: gravity = "center" z Twój układ dla @ + id/textView1 i spróbuj ... – Vamshi

+0

Zmień szerokość 'TextView' na' match_parent' lub użyj 'android: layout_gravity' zamiast tego. –

+0

Spójrz na te pytania: [link] (http://stackoverflow.com/questions/3482742/android-gravity-and-layout-gravity) – andysando

Odpowiedz

20

ustawić ten tekst zobaczyć szerokość "wrap_content" oznacza to, co kiedykolwiek tekst jest, widok przyjmuje rozmiar tekstu.

aw LinearLayout domyślny grawitacja (stosowane tutaj) jest 'centrum'

należy spróbować tego:

<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% czasu, not working properly == not used properly.

Pomylisz się z gravity i layout_gravity.

gravity to sposób w jaki tekst dopasuje się do siebie w TextView. TextView jest w wrap_content to nic nie robi, ponieważ TextView jest dokładnie wielkością tekstu.

layout_gravity jest sposób TextView dostosuje się w jego rodzica, w danym przypadku w pionowym LinearLayout

+0

@Harmen 'gravity' jest atrybutem zdefiniowanym tylko dla' TextView'. – njzk2

1

dałeś swoją szerokość TextView wrap_content, i to jest problem, sprawdź poniżej kodu i zastąpienie go do listy kod.

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

ustawić android:layout_width="fill_parent" dla textView1

0

Upewnij się, że nie ma czegoś takiego, że kręci

app:layout_box="left|top" 
Powiązane problemy