为什么我的文字与左边对齐?

时间:2017-07-09 13:46:52

标签: java android

我是Android开发新手,我无法解决这个问题。当我构建这个应用程序时,我的初始文本居中,但是当我单击按钮或长按它时,响应文本保持对齐。此外,双击,滚动和投掷手势根本不起作用。可能是什么原因?

MainActivity.java:

package com.revolise.gesturehandling;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;

public class MainActivity extends AppCompatActivity implements
GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener
    {

        private Button button1;
        private GestureDetectorCompat gestureDetect;
        private TextView txt1;


        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            button1 = (Button)findViewById(R.id.button1);
            txt1 = (TextView)findViewById(R.id.textView1);
            this.gestureDetect = new GestureDetectorCompat(this, this);
            gestureDetect.setOnDoubleTapListener(this);

            button1.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            txt1.setText("clicked to the button whooaoaaa");
                        }
                    }
            );

            button1.setOnLongClickListener(
                    new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View view) {
                            txt1.setText("long clicked to the button woww");
                            return false;
                        }
                    }
            );


        }
        @Override
        public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent motionEvent) {
            txt1.setText("Hey, you double tapped");
            return true;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent motionEvent) {
            txt1.setText("Aaaand now you're swiping.");
            return true;
        }

        @Override
        public boolean onDown(MotionEvent motionEvent) {
            return true;
        }

        @Override
        public void onShowPress(MotionEvent motionEvent) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent motionEvent) {
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            txt1.setText("Scrolling weee");
            return true;
        }

        @Override
        public void onLongPress(MotionEvent motionEvent) {
            txt1.setText("Long pressed");

        }

        @Override
        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            txt1.setText("Whoaa flinginggg");
            return true;
        }
}


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.revolise.gesturehandling.MainActivity">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="160dp"
    android:text="@string/initial_text"
    android:textSize="30sp"
    app:layout_constraintBottom_toTopOf="@+id/button1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="100dp"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.501"
    android:layout_marginBottom="200dp" />
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

对于第一个问题(错误的文字对齐)

您应将TextView宽度设置为match_parent,将gravity设置为center_horizo​​ntal。所以你的代码应该是这样的:

<TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="160dp"
        android:text="@string/initial_text"
        android:textSize="30sp"
        android:layout_gravity="center_horizontal"
        app:layout_constraintBottom_toTopOf="@+id/button1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp" />