参考URL
メモ
- 上のブログ記事ではJavaからaddView()を呼んでいますが、代わりに以下のサンプルのようにリソースで子要素にしてもよいです。
- APIレフェレンスに書いてある通りViewSwitcherは子要素を2つか持てません。3つ目の子要素を定義するとRuntimeExceptionが出ました。
- リソースのXML内で
android:inAnimation="@android:anim/fade_in"のように属性を指定すればアニメーションできます。ただし、これをつけるとEclipseでLayoutタブに切り替えたときにNotFoundException: Could not find anim resource matching value 0x01A0000 (resolved name: fade_in) in current configuration.と表示されてプレビューが表示できません。まあ、どのみちxmlのタブでソースを直接編集しますので致命的ではないです。
src/sample/viewswitcher2.ViewSwitcher2.java
package sample.viewswitcher2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ViewSwitcher;
public class ViewSwitcher2 extends Activity implements OnClickListener {
private ViewSwitcher mSwitcher1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSwitcher1 = (ViewSwitcher) findViewById(R.id.switcher1);
Button showAnswerButton = (Button) findViewById(R.id.switch_button);
showAnswerButton.setOnClickListener(this);
Button goNextButton = (Button) findViewById(R.id.next_button);
goNextButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mSwitcher1.showPrevious();
}
});
}
@Override
public void onClick(View v) {
// 2つ目のビューでshowPrevious()を呼ばずにshowNext()を呼んでも1つ目に戻る。
mSwitcher1.showNext();
}
}
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ViewSwitcher android:id="@+id/switcher1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:inAnimation="@android:anim/fade_in" android:outAnimation="@android:anim/fade_out">
<Button android:id="@+id/switch_button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/show_answer" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/this_is_the_answer" />
<Button android:id="@+id/next_button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/go_next" />
</LinearLayout>
<!-- TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="third view" / -->
</ViewSwitcher>
</LinearLayout>
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ViewSwitcher2</string>
<string name="show_answer">Show Answer</string>
<string name="this_is_the_answer">This is the answer!</string>
<string name="go_next">Go Next</string>
</resources>
0 件のコメント:
コメントを投稿