9

文字列形式の大きなストーリーがあります。ギャラリーにテキストを表示したい。私がやりたいのは、ギャラリー内のすべてのビューに画面に収まるテキストが表示されるように、すべてのテキストをスライスすることです。

文字列を部分的に作成できるように、各部分が画面に表示され、各部分が画面全体をカバーします。

注意すべき点の1つは、ユーザーがテキストサイズを大、小に変更できるため、サイズの変更に応じて画面上のテキストも変更されることです。

これを行う方法があるかどうか疑問に思っています。

解決

userSeven7sにご協力いただきありがとうございます。あなたの例に基づいて、私は例を作ることができます。ここにあります:

package com.gsoft.measure.text;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainScreen extends Activity {

    private final String TAG = "MainScreen";
    private String textToBeShown = "These are the text";
    private String sampleText = "Here are more text";
    private TextView mTextView = null;

    Handler handler = new Handler() {

        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                updateUI();
            }
        };
    };

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

        mTextView = (TextView) findViewById(R.id.ui_main_textView);
        mTextView.setTextSize(20f);
        for (int i = 0; i < 100; i++) {
            textToBeShown = textToBeShown + " =" + i + "= " + sampleText;
        }

        // I am using timer as the in UI is not created and
        // we can't get the width.
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                // So that UI thread can handle UI work
                handler.sendEmptyMessage(1);
            }
        };
        Timer timer = new Timer();
        timer.schedule(task, 1000 * 1);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void updateUI() {

        // Set text
        mTextView.setText(textToBeShown);
        // Check the width
        Log.e(TAG, "Width = " + mTextView.getWidth());

        // Check height of one line
        Log.e(TAG, "Line height= " + mTextView.getLineHeight());

        // Check total height for TextView
        Log.e(TAG, "Text height= " + mTextView.getHeight());

        // No of line we can show in textview
        int totalLine = mTextView.getHeight() / mTextView.getLineHeight();
        Log.e(TAG, "Total Lines are height= " + totalLine);


        for (int i = 0; i < totalLine; i++) {
            // Get No of characters fit in that textView
            int number = mTextView.getPaint().breakText(textToBeShown, 0, textToBeShown.length(), true,
                    mTextView.getWidth(), null);
            Log.e(TAG, "Number of chracters = " + number);

            // Show the text that fit into line
            Log.e(TAG, textToBeShown.substring(0, number));
            // Update the text to show next
            textToBeShown = textToBeShown.substring(number, textToBeShown.length());
        }
    }
}

これが私のXMLです

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_id_for_value"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/black"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/ui_main_textView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/twitter"
        android:textColor="@color/white" />

</LinearLayout>
4

3 に答える 3

7

ソースコードをチェックしてTextView、文字列を省略できる場所を決定する方法を確認します。

のコードTextViewこちらです。

TextUtilsまたは、クラスの public static CharSequence ellipsize(CharSequence text, TextPaint p, float avail, TruncateAt where)メソッドを使用することもできます。

TextPaint pTextViewのペイントオブジェクトである必要があります。

アップデート:

もう1つの方法は、を使用することPaint.getTextWidths(char[] text, int index, int count, float[] widths)です。

textpaint.getTextWidths(char[] text, int index, int count, float[] widths);

int i = 0;
int prev_i = 0;
while (i < count) {
    textWidth = 0;
    for (int i = prev_i; (i < count) || (textWidth < availableWidth); i++) {
        textWidth += widths[i];
    }
    String textThatFits = mOriginalText.subString(prev_i, i);
    mTextview.setText(textThatFits);
    prev_i = i;
}

iTextViewに収まる文字数です。はピクセル単位
availableWidthの幅です。TextView

このコードは概算であり、構文エラーが含まれています。それを機能させるには、いくつかの小さな変更を行う必要があります。

アップデート2:

別の代替手段は、

int breakText (CharSequence text,      
                int start, int end,   
                boolean measureForwards,   
                float maxWidth, float[] measuredWidth). 

これがあなたにとって最良の解決策だと思います。こちらのドキュメントを確認してください。

アップデート:

ペイントを使用したサンプルコード。breakText方法。

paint.setSubpixelText(true);
int prevPos = 0;
while (nextPos  < chars.length) {
    int nextPos = paint.breakText(chars, prevPos, chars.length, maxWidth, null);
    tvStr = str.substring(prevPos, nextPos);
    prevPos = nextPos+1;
}
于 2012-05-05T18:27:56.313 に答える
0

各部分の文字数が異なる場合は、フォントのサイズも異なる必要があります。それだけでなく、テキストが小さいスペースに収まる必要があるため、デバイスが小さすぎるとフォントサイズも小さくなります。十分なスペースがない場合は下にスクロールできるようにしながら、すべてのパーツで同じフォントサイズを使用することをお勧めします。

このために、各ページにスクロールビューを備えたビューページャーを使用できます。各ページにはテキストビューが含まれています。

于 2012-05-05T17:01:12.337 に答える
0

Graphics.getFontMetricsおよびFontMetrics.stringWidthは、画面上のテキストの実際のサイズを決定するのに役立ちます。この計算に基づいて、表示される部分文字列の長さを決定できます。

于 2012-05-05T17:05:42.067 に答える