2013-09-26 37 views
9

Có gì sai với mã của tôi, tôi đang cố hiển thị TextView có tên "invalid", ở các vị trí khác nhau (trái, phải, giữa), nhưng trọng lực (trái, phải, trung tâm) thắng ' t làm việc! enter image description hereTextview Gravity không hoạt động bình thường trong android

text.xml tôi là

<?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 tôi là

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 android: layout_gravity = "center" android: trọng lực = "center" từ bố cục của bạn cho @ + id/textView1 và thử ... – Vamshi

+0

Thay đổi chiều rộng của 'TextView' thành' match_parent' hoặc sử dụng 'android: layout_gravity'. –

+0

Hãy xem các câu hỏi này: [link] (http://stackoverflow.com/questions/3482742/android-gravity-and-layout-gravity) – andysando

Trả lời

20

Bạn đặt văn bản này xem có chiều rộng "wrap_content" nó có nghĩa là, những gì bao giờ văn bản là, xem có kích thước của văn bản.

và trong LinearLayout, mức độ nghiêm trọng mặc định (sử dụng ở đây) là 'trung tâm'

bạn nên thử điều này:

<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% thời gian, not working properly == not used properly.

Bạn đang nhầm gravitylayout_gravity.

gravity là cách văn bản sẽ căn chỉnh chính nó trong TextView. Các TextView được trong wrap_content này không có gì, vì TextView chính xác là kích thước của văn bản.

layout_gravity là cách các TextView sẽ liên kết chính nó trong mẹ của nó, trong trường hợp của bạn trong dọc LinearLayout

+0

@Harmen 'gravity' là một thuộc tính được định nghĩa chỉ cho' TextView'. – njzk2

1

Ngài đã ban cho wrap_content rộng TextView của mình, và đó là vấn đề, Kiểm tra bên dưới mã và thay thế nó để bạn mã.

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

thiết android:layout_width="fill_parent" cho textView1

0

Hãy chắc chắn rằng bạn không có một cái gì đó như thế treo xung quanh

app:layout_box="left|top" 
Các vấn đề liên quan