setGravity не работает - PullRequest
       4

setGravity не работает

1 голос
/ 20 декабря 2011

Пожалуйста, смотрите мой код ниже.Я не могу заставить SetGravity работать.

Как получилось?

Код макета:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="25dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:longClickable="true"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/etCommands"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Type a command"
        android:password="true" />


    <LinearLayout
        android:weightSum="100"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <Button
        android:layout_weight="20"
        android:id="@+id/btnResults"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Results" />

    <ToggleButton
        android:paddingBottom="10dp"
         android:layout_weight="80"
        android:id="@+id/passTog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ToggleButton" android:checked="true"/>

    </LinearLayout>

    <TextView
        android:id="@+id/tvResults"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="invalid" />

</LinearLayout>

Java-код:

public class TextPlay extends Activity {

    private static final String TAG = "MyApp";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.text);

        Button chkCmd = (Button) findViewById(R.id.btnResults);
        final ToggleButton passTog = (ToggleButton) findViewById(R.id.passTog);
        final EditText input = (EditText) findViewById(R.id.etCommands);
        final TextView display = (TextView) findViewById(R.id.tvResults);

        passTog.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub

                if (passTog.isChecked()) {

                    input.setInputType(InputType.TYPE_CLASS_TEXT
                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);

                } else {

                    input.setInputType(InputType.TYPE_CLASS_TEXT);

                }

            }
        });

        chkCmd.setOnClickListener(new View.OnClickListener() {

                @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String check = input.getText().toString();

                Log.i(TAG, "Check Value is: " + check);

                if (check.contentEquals("left")) {
                    Log.i(TAG, "I am under left ");
                    display.setGravity(Gravity.LEFT); //WHYW ILL YOU NOT WORK SETGRAVITY?????????????
                } else if (check.contentEquals("center")) {
                    display.setGravity(Gravity.CENTER);

                } else if (check.contentEquals("right")) {
                    display.setGravity(Gravity.RIGHT);
                }

                else if (check.contentEquals("blue")) {
                    display.setGravity(Gravity.RIGHT);
                }

            }
        });
    }
}

Ответы [ 12 ]

0 голосов
/ 22 декабря 2011

установить layout_width="wrap_content" и установить гравитацию макета.

0 голосов
/ 20 декабря 2011

Попробуйте установить layout_gravity="center" и layout_width="wrap_ontent", может быть, это будет сделано:)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...