Textview Gravity在android中无法正常工作 [英] Textview Gravity not working properly in android

查看:79
本文介绍了Textview Gravity在android中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有什么问题,我试图在不同的位置(左,右,中)显示我的名为"invalid" 的TextView,但在重力(左,右,中)显示将无法正常工作!

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!

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

推荐答案

您将此文本视图设置为" wrap_content "的宽度,这意味着,无论文本是什么,该视图的大小为文字.

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.

,在 LinearLayout 中,默认重力(在此使用)为' center '

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

您应该尝试以下方法:

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

这篇关于Textview Gravity在android中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆