【问题标题】:TextView scrolling does not workTextView 滚动不起作用
【发布时间】:2017-12-21 12:48:37
【问题描述】:

这是一个带有TextView 的缩放应用程序。我想放大TextView 的内容。

public class sukharta extends AppCompatActivity {
    TextView scaleGesture;
    ScaleGestureDetector scaleGestureDetector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sukharta);
        scaleGesture = (TextView)findViewById(R.id.textView);
        scaleGesture.setText(R.string.sukharta);
        scaleGestureDetector = new ScaleGestureDetector(this, (ScaleGestureDetector.OnScaleGestureListener) new simpleOnScaleGestureListener());
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getLayoutInflater().inflate(R.layout.activity_sukharta, (ViewGroup) menu);
        return true;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        scaleGestureDetector.onTouchEvent(event);
        return true;
    }

    public class simpleOnScaleGestureListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            // TODO Auto-generated method stub
            float size = scaleGesture.getTextSize();
            Log.d("TextSizeStart", String.valueOf(size));

            float factor = detector.getScaleFactor();
            Log.d("Factor", String.valueOf(factor));


            float product = size*factor;
            Log.d("TextSize", String.valueOf(product));
            scaleGesture.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);

            size = scaleGesture.getTextSize();
            Log.d("TextSizeEnd", String.valueOf(size));
            return true;
        }
    }
}

sukharta.xml 文件

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</ScrollView>
</LinearLayout>

缩放适用于没有ScrollViewTextView,但内容超出框架,我无法向下滚动查看。 但是当我添加ScrollView 时,我无法缩放。请帮忙。

【问题讨论】:

    标签: java android xml android-scrollview pinchzoom


    【解决方案1】:

    试试这个设置你的滚动视图属性android:fillViewport="true"

    <ScrollView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fillViewport="true">
    
     <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
        <TextView
           android:id="@+id/textView"
           android:layout_width="match_parent"
           android:layout_height="match_parent"/>
     </LinearLayout>
    
    </ScrollView>
    

    【讨论】:

    • 如果我使用它,我无法缩放文本视图。
    【解决方案2】:

    你可以试试:

    <TextView
        android:id="@+id/textView"
        android:scrollbars="vertical"
        android:maxLines="LINES"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    textView.setMovementMethod(new ScrollingMovementMethod());
    

    【讨论】:

    • 对我没有帮助。活动只是崩溃
    • java.lang.RuntimeException:无法启动活动 ComponentInfo{net.androidsrc.videosplash/net.androidsrc.videosplash.sukharta}:java.lang.NullPointerException:尝试调用虚拟方法 'void android. widget.TextView.setMovementMethod(android.text.method.MovementMethod)' 在空对象引用上
    • 你的textview好像没有初始化
    • 我已经初始化了textview。请检查顶部的代码。
    • 活动现在加载正常。但是缩放有问题。无法缩放
    【解决方案3】:

    可能是 NestedScrollView 对你有用,试试上面的代码。

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
       <LinearLayout
    
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
    

    【讨论】:

    • 活动崩溃
    • 还是一样。活动加载正常,但我无法缩放。
    • 我尝试向 textview 添加更多内容。一切正常,但问题在于现在的缩放。缩放不起作用
    • 尝试使用调试器解决代码调用 onTouchEvent 是否有效...
    猜你喜欢
    • 2023-04-05
    • 2013-11-11
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 2016-05-29
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多