如何缩放和滚动文本视图?

时间:2013-07-02 06:50:56

标签: android scroll zoom textview

我无法实现textview的滚动和缩放。 我看了类似的问题,但无法得到任何令人满意的解决方案。 我知道webview更容易实现,但它无法对其中的某些关键字执行字体颜色更改。

我的代码如下。我只实现了滚动:

package com.example.myclass;


import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MotionEvent;
import android.widget.TextView;


public class viewProgramActivity<x> extends Activity{

    private SQLiteDatabase database;
    ExternalDbOpenHelper dbOpenHelper;
    private static TextView textView;
    private static final String DB_NAME = "umangdb.db";
    private String rowid;
    private String programtext;
    ScaleGestureDetector scaleGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewprogram);

        registerForContextMenu(findViewById(R.id.textView1));

        dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
        database = dbOpenHelper.openDataBase();

        Intent intent=getIntent();
        rowid=intent.getStringExtra("myextra");

        String query="select * from engg_table where _id="+rowid;
        Cursor myCursor = database.rawQuery(query,null);
        myCursor.moveToFirst();

        programtext=myCursor.getString(4);
        myCursor.close();

        textView  = (TextView) findViewById(R.id.textView1);
        textView.setText(programtext);




        textView.setMovementMethod(new ScrollingMovementMethod());


        String [] keywordsred={"int","char","{","}"};
        for(String y:keywordsred)
        {
        fontcolor(y,0xffff0000);
        }

        String [] keywordspurple={"printf","scanf","#include<stdio.h>","%d","%f","%s","%c"};
        for(String y:keywordspurple)
        {
        fontcolor(y,0xFF8B008B);
        }

    }

    private void fontcolor(String text,int color) {
        textView=(TextView)findViewById(R.id.textView1);
        Spannable raw=new SpannableString(textView.getText());

        int index=TextUtils.indexOf(raw, text);
        while (index >= 0) {
          raw.setSpan(new ForegroundColorSpan(color), index, index
              + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
          index=TextUtils.indexOf(raw, text, index + text.length());
        }
        textView.setText(raw);

      }



   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.optionsmenu, menu);

        return true;
    }

}   

对应的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/uecp" />

</RelativeLayout>

应该添加什么代码来启用缩放,我尝试了很多方法但是未能实现。我是java和android的初学者。请详细解释

1 个答案:

答案 0 :(得分:0)

通过放大TextView,意味着增加/减少TextView

的字体大小

你可以refer here。它会更改onTouchEvent事件上的textView的字体大小。

希望这会有所帮助..