Java Textview Gravity 在 android 中无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19025301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:28:57  来源:igfitidea点击:

Textview Gravity not working properly in android

javaandroidlayout-gravity

提问by Krish

What's wrong with my code, I'm trying to display my TextView named "invalid", in different locations (left,right,center), but the gravity (left,right,center) won't work! enter image description here

我的代码有什么问题,我试图"invalid"在不同的位置(左、右、中心)显示名为 的 TextView ,但重力(左、右、中心)不起作用!在此处输入图片说明

My text.xml is

我的 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>

My TextPlay.java is

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

采纳答案by Guian

You set this text view a width of "wrap_content" it means, what ever the text is, the view take the size of the text.

您将此文本视图设置为“ wrap_content”的宽度,这意味着无论文本是什么,视图都会采用文本的大小。

and in the LinearLayout, the default gravity (used here) is 'center'

并且在 中LinearLayout,默认重力(此处使用)是“中心

you should try this :

你应该试试这个:

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

回答by njzk2

99% of the time, not working properly== not used properly.

99% 的情况下,not working properly== not used properly

You are mistaking gravityand layout_gravity.

你错了gravitylayout_gravity

gravityis the way the text will align itself in the TextView. The TextViewbeing in wrap_contentthis does nothing, as the TextViewis exactly the size of the text.

gravity是文本TextView. 将TextView在存在wrap_content这种什么都不做,因为TextView正是文本的大小。

layout_gravityis the way the TextViewwill align itself in its parent, in your case in the vertical LinearLayout

layout_gravityTextView将自己在其父级中对齐的方式,在您的情况下是垂直的LinearLayout

回答by Pratik Dasa

You have given your TextView width wrap_content, and that is the problem, Check below code and replace it to your code.

您已经给了 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"   
/>

回答by Pratik Dasa

set android:layout_width="fill_parent"for textView1

android:layout_width="fill_parent"为 textView1设置

回答by MonoThreaded

Make sure you don't have something like that hanging around

确保你没有这样的东西闲逛

app:layout_box="left|top"