ラベル TextView の投稿を表示しています。 すべての投稿を表示
ラベル TextView の投稿を表示しています。 すべての投稿を表示

2012年8月16日木曜日

TextView・一行表示・横スクロール

TextView で一行で表示される文字数を超えると自動的に改行される。


一行に制限する。
android:singleLine="true"


横スクロールさせる

<LinearLayout 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"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ" />

        <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="ABCDEFGHIJKLMNOPQRSTUVWXYZ" />

</LinearLayout>

はみ出さない場合は、スクロールしない






2012年6月17日日曜日

文字の大きさの指定:Android setTextSize

Android で文字の大きさについて調べてみる。

setTextSize() の単位は SPが使われているらしい。
例:
setTextSize (20);

sp(scaled pixel):解像度に応じて伸縮する

dp (dip:Density-independent Pixels ):端末のdpiによってサイズが切り替わる。
160 dpi の画面で 1 dp = 1 px

レイアウトにはdpを使ってテキストはspで記述すると同じ表示になるそうです。

sp,dp から px の変更
20sp x 1.5(scaledDensity) = 30px //未確認

scaledDensityの調べ方

// ディスプレイ情報の取得
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
         
Log.d("test", "density=" + metrics.density);  
Log.d("test", "densityDpi=" + metrics.densityDpi);  
Log.d("test", "scaledDensity=" + metrics.scaledDensity);  
Log.d("test", "widthPixels=" + metrics.widthPixels);  
Log.d("test", "heightPixels=" + metrics.heightPixels);  
Log.d("test", "xDpi=" + metrics.xdpi);  
Log.d("test", "yDpi=" + metrics.ydpi);  



2011年11月17日木曜日

値の設定-setText

        TextView textView = (TextView) findViewById(R.id.editText1);
        textView.setText("設定する文字列");

2011年6月28日火曜日

文字サイズの変更

XMLファイルで指定する場合
android:textSize="24sp"

ソースで指定する場合
btn.setTextSize(24.0f);

2011年6月8日水曜日

値の取得-getText

TextView に設定してあるテキストを取得して、
ダイアログで表示する。

package com.GetTextTest;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.TextView;

public class GetTextTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView textView = (TextView) findViewById(R.id.hello);
        
        new AlertDialog.Builder(this).setMessage(textView.getText()).show();
        
    }
}