Java Textview重力在android中无法正常工作

Java Textview重力在android中无法正常工作,java,android,layout-gravity,Java,Android,Layout Gravity,我的代码有什么问题,我试图在不同的位置(左、右、中)显示名为“无效”的文本视图,但重力(左、右、中)不起作用 我的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

我的代码有什么问题,我试图在不同的位置(左、右、中)显示名为“无效”的文本视图,但重力(左、右、中)不起作用

我的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>
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);
                }
            }
        });
    }
}

您将此文本视图的宽度设置为“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
/>

99%的时间,
工作不正常
=
使用不正常

你弄错了重力
和重力布局

gravity
是文本在
文本视图中对齐的方式。
TextView
位于
wrap\u内容中
这不起任何作用,因为
TextView
正是文本的大小


layout\u gravity
TextView
在父视图中对齐的方式,在垂直
LinearLayout
中,您已经给出了TextView的宽度换行内容,这就是问题所在,请检查下面的代码并将其替换为您的代码

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


set
android:layout\u width=“fill\u parent”
用于文本视图1

请确保没有类似的东西

app:layout_box="left|top"

删除android:layout\u gravity=“center”android:gravity=“center”从@+id/textView1的布局中,尝试…将
TextView
的宽度更改为
match\u parent
或使用
android:layout\u gravity
替代。同时看看这个问题:为什么要在代码中使用选项卡?它们浪费了代码大小…@Harmen
gravity
是仅为
TextView