2013-09-26 30 views
9

我的代碼有什麼問題,我試圖在不同位置(左,右,中心)顯示名爲"invalid"的TextView,但重力(左,右,中)工作! enter image description hereTextview重力在android中不能正常工作

我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

將'TextView'的寬度更改爲'match_parent'或者使用'android:layout_gravity'來代替。 –

+0

看看這個問題以及:[鏈接](http://stackoverflow.com/questions/3482742/android-gravity-and-layout-gravity) – andysando

回答

20

你把這句話查看 「WRAP_CONTENT」 的寬度意味着什麼無論文本是什麼,視圖都取決於文本的大小。

,並在LinearLayout,默認的重力(這裏使用)是 '中心'

你應該試試這個:時間

<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

你錯了gravitylayout_gravity

gravity是文本將自己對齊的方式TextViewTextViewwrap_content這不做什麼,因爲TextView正是文本的大小。

layout_gravityTextView將調整本身在其父的方式,在你的情況下,在垂直LinearLayout

+0

@Harmen'gravity'是僅爲'TextView'定義的屬性。 – njzk2

1

你給你的TextView的寬度WRAP_CONTENT,那就是問題,檢查下面的代碼,並將其替換成您碼。

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

設置android:layout_width="fill_parent"爲textView1

0

確保你沒有類似的東西掛在

app:layout_box="left|top"