スポンサーサイト
[Android] 画面の下部にボタンを配置する方法
Android で画面の下部にボタンを配置したかったのだが、しっくり来る方法を見いだすまでに時間がかかってしまったのでメモしておく。
以下のように、画面の下部に2つのボタンを置き、画面の上部には Spinner(でなくても良いが)を置いて、その間の隙間を ListView で埋めるというレイアウトである。
![[Android] 画面の下部にボタンを配置する方法](http://blog-imgs-42.fc2.com/a/k/a/akabanessa/tmp.png)
ついでに、擬似的な背景画像を画面の右下に置いている。
背景画像を右下に置くというのは、実は非常に難しかった...
もっと良い方法はないのだろうか。
この方法で背景に画像を置いた場合、そのままだとListViewをスクロールした時に、背景画像がチラチラ消えたり付いたりしてしまう。
それを防ぐためには、
ListView#setScrollingCacheEnabled(false);
を呼んでおくこと。
(追記:.xml ファイル内で、android:scrollingCache="false" と指定しても良い)
この画面を実現するための main.xml ファイルは以下のようになっている。
以下のように、画面の下部に2つのボタンを置き、画面の上部には Spinner(でなくても良いが)を置いて、その間の隙間を ListView で埋めるというレイアウトである。
![[Android] 画面の下部にボタンを配置する方法](http://blog-imgs-42.fc2.com/a/k/a/akabanessa/tmp.png)
ついでに、擬似的な背景画像を画面の右下に置いている。
背景画像を右下に置くというのは、実は非常に難しかった...
もっと良い方法はないのだろうか。
この方法で背景に画像を置いた場合、そのままだとListViewをスクロールした時に、背景画像がチラチラ消えたり付いたりしてしまう。
それを防ぐためには、
ListView#setScrollingCacheEnabled(false);
を呼んでおくこと。
(追記:.xml ファイル内で、android:scrollingCache="false" と指定しても良い)
この画面を実現するための main.xml ファイルは以下のようになっている。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageView1"
android:src="@drawable/bkgnd_main"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/linearLayout1"
/>
<LinearLayout
android:id="@+id/linearLayout2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/linearLayout1"
>
<Spinner
android:id="@+id/spinner1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="1"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
コメント
コメントの投稿
« [Android] 日付を文字列に変換する分かりやすく簡単な方法 l ホーム l [Eclipse] Eclipseで日本語入りのJavaソースコードが文字化けする場合の対処方法 »



