fc2ブログ

戌印-INUJIRUSHI- (Androidあれこれ)

Androidのプログラミングをメインにしてます。記事に貼られたソースコードはダブルクリックすることで行番号をはずしてコピーすることができます。

 
1
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
25
26
27
28
29
30
31
03

レイアウトXMLで onClick を設定する

以前『ウィジェットにイベントを設定』で Java クラスから onClick イベントを設定する方法を書きました。
今回はレイアウトXMLからの設定方法について。

レイアウトXMLからクリックイベントを設定するには android:onClick="[メソッド名]" を使用します。

android:onClick="[メソッド名]" から呼ぶメソッドは以下の条件を満たす必要があります。
メソッドは呼び出した Activity クラスに実装する。
・Fragment から呼び出す場合もメソッドは Activity クラスに実装する。
メソッドは public でなければならない。static 可。
引数は View のみ。引数なし、または、他の引数を設定することはできない。

条件を満たさない場合、クリック時に java.lang.NoSuchMethodException が発生します。

以下、サンプルコード。
activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="testClick"
android:text="click" />
</LinearLayout>

TestActivity.java
package jp.inujirushi.android.sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class TestActivity extends Activity {

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

public void testClick(View view) {
Toast.makeText(this, "test", Toast.LENGTH_LONG).show();
}
}

ウィジェットにイベントを設定』から1年8ヶ月も経っているからすごい今更の内容…
スポンサーサイト



ScrollViewの余白の不具合 (Android 2.3.3 GingerBread 以下)

Android ではレイアウトをスクロールさせる場合、ScrollView を使います。

しかし、この ScrollView には余白 (android:layout_marginandroid:padding) に不具合があります。
不具合の内容は以下のとおり。


概要:
 ScrollView の余白が正しく設定されない

バージョン:
 Android 2.3.3 以下

 ※ Android 3.* (API 11~13 / HanyComb) はエミュレータで確認したところ正常な結果となりました。
  ただし、Graphical Layout では誤った結果、それも Android 2.3.3 以下とは違った表示になります。
  実機では動作確認をしていませんが、エミュレータで正しく表示されたのでとりあえずOKとしています。

詳細:
 ScrollView 直下の View の android:layout_margin, android:padding が正しく反映されない。
 top, bottom の余白は無効となり、left の余白は right に加算される。

実験:
 以下のレイアウト構成に上から順に layout_margin="10dp" を設定する。

ScrollView (#ffffff)
└ LinearLayout (#cccccc)
 └ TextView (#ff8080)

正しい結果 (Android 4.2 / API 17 / JellyBean)
android_scroll_margin_api17.png

誤った結果 (Android 2.3.3 / API 10 / GingerBread)
android_scroll_margin_api10.png

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#cccccc" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff8080"
android:text="ScrollView margin" />
</LinearLayout>
</ScrollView>

<View
android:layout_width="match_parent"
android:layout_height="10dp" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cccccc" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#ffffff" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff8080"
android:text="LinearLayout margin" />
</LinearLayout>
</ScrollView>

<View
android:layout_width="match_parent"
android:layout_height="10dp" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cccccc" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#ff8080"
android:text="TextView margin" />
</LinearLayout>
</ScrollView>

</LinearLayout>

対応:
 実験の結果から ScrollView 直下のみ余白が正しく設定されないことがわかります。
 そこで余白は ScrollView 自身か、階層が深くなってしまうが ScrollView 直下にレイアウトを追加してそのレイアウトに View or ViewGroup を設定すればバージョンに関係なく正しく表示されるようになります。

LinearLayout を使って均等割り付け : layout_weight

Android のレイアウトで均等割付を行うには LinearLayoutandroid:layout_weight を使います。

この layout_weight は View に対するウェイト(重み)を指定し、余白を割り当てます

余白を割り当てるため、均等割りを行うのであればサイズ指定を 0dp にする必要があります。
vertical (縦並び)であれば android:layout_height="0dp"
horizontal (横並び)であれば android:layout_width="0dp"

android_layout_weight_0dp.png

割り当てられるサイズは『 余白 × layout_weight ÷ layout_weight の合計 』になります。
layout_weigh が定義されていない、または 0 の時、余白は割り当てられません。

もしサイズ指定を 0dp 以外にした場合、指定したサイズ領域が確保された後に余白が割り振られるため、均等割りにはなりません。
例) layout_width="wrap_content"
android_layout_weight_wrap.png

以下、いつものサンプルソースコード。

■ 画面イメージ
Android_layout_weight.png


■ XML で定義
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cccccc"
android:padding="5dp" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ff8080"
android:text="均等割付 1/2" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#8080ff"
android:text="均等割付 1/2" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cccccc"
android:gravity="center"
android:padding="5dp" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ff8080"
android:text="均等割付 1/3" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#8080ff"
android:text="均等割付 1/3" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#80ff80"
android:text="均等割付 1/3" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cccccc"
android:gravity="center"
android:padding="5dp" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ff8080"
android:text="均等割付 1/3" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#8080ff"
android:text="均等割付 2/3" />
</LinearLayout>

</LinearLayout>



■ Java で定義 (1段目のみ)
package jp.inujirushi.android.sample;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class TestLayoutActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// padding を計算する
float density = getResources().getDisplayMetrics().density;
int padding = (int) (5.0f * density + 0.5f);

// レイアウトを生成する
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout.setBackgroundColor(Color.argb(0xff, 0xcc, 0xcc, 0xcc));
layout.setPadding(padding, padding, padding, padding);

// layout_weight を指定した LayoutParams を生成する
// 引数は左から順に width, height weight
LinearLayout.LayoutParams params = new LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);

// TexiView 1 を生成する
TextView text1 = new TextView(this);
text1.setText("均等割付 1/2");
text1.setBackgroundColor(Color.argb(0xff, 0xff, 0x80, 0x80));
text1.setLayoutParams(params);
layout.addView(text1);

// TexiView 2 を生成する
TextView text2 = new TextView(this);
text2.setText("均等割付 1/2");
text2.setBackgroundColor(Color.argb(0xff, 0x80, 0x80, 0xff));
text2.setLayoutParams(params);
layout.addView(text2);

// レイアウトを追加する
setContentView(layout);
}
}

プロフィール

とむ・やむくん

Author:とむ・やむくん
管理人について

Windows 7 / 64bit
Eclipse 4.2 Juno (日本語パッチ済)

スポンサーサイト
最新トラックバック
検索フォーム
ブロとも申請フォーム
QRコード
QR
Twitter
2013/01/04 19:00 カウント開始