とうとうjava側の作りに入ります。

今回はアラート表示を行えるToastを紹介。
パンですね。ご飯派です。

package atm.sample.layout;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;

public class MainActivity extends Activity
{

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_vh);
this.showMsg();
}

private void showMsg(){

Toast.makeText(this, "MESSAGE", Toast.LENGTH_LONG).show();
}
}


該当箇所は「Toast.makeText()」です。
第一引数にContext
第二引数に表示したいメッセージ
第三引数に表示時間
を指定します。

第三引数の表示時間はToastクラスに定数が用意されているので、
LENGTH_(LONG/SHORT)を指定してください



見辛いですが、キーボード表示の上に「MESSAGE」と出てますね。

ちょっとしたデバッグやユーザへの簡単な通知に便利です。
*最後にshow()を書き忘れないように気を付けてください。





今日のネタはweightです。
重さが重要なんです。

さっそくxmlです。

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

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="TEST1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

<Button android:text="TEST3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<Button android:text="TEST4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />

<Button android:text="TEST6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

</LinearLayout>


ボタン、テキストボックス(EditText)、ボタンを要素にもつ
LinearLayoutを2行出します。

この時、テキストボックスの横幅を画面全体にしています。
ただし、2行目はandroid:layout_weight="1"と記載



1行目は右側のボタンが消えてしまいました。
(EditTextに押し出されてしまった。)

2行目はキレイに画面内に収まっています。

android:layout_weight="1"とすると、
余ったスペースを全部使う事を意味します。
また、逆に余ったスペースしか使わないとの意味でもあります。

大きい画像を表示する時などに指定するといいですね。


	<LinearLayout 
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<ListView
android:id="@+id/listAll"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
>
</ListView>
<ListView
android:id="@+id/listSelect"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
>
</ListView>
</LinearLayout>


上記のようにwidthに0dpiを指定してweightを1にした要素を並べると画面を半々で使えます。

実際にゲームを落として楽しみながら確認してみてください
https://play.google.com/store/apps/details?id=atm.game.hitAndBlowCollection
(とうとう宣伝開始w)






ブログとかSNSを継続利用するのが大嫌いな僕がまさかの第2回

酔ってるので、細かいことは気にしないでください

はい、xmlです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"

android:gravity="left">

<Button android:text="TEST1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:text="TEST2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:text="TEST3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>


何か初見のgravityとか言う奴が出てきましたね。
重力かよ!?

引っ張ると自分が面倒なので進めると
「子要素の位置を自身の範囲内で指定できます。」
ニホンゴめんどくせー
要はleftって指定したら自分の表示範囲内で左に
rightって指定したら自身の範囲内で右に
詰めて表示します。

実際の表示

left指定(指定なしと同じやん)


right指定



はい、ここでダルいポイントが出てきます。
左右はleft,rightでいいのが分かった
上下はtop,bottomで良さそうな気がする
じゃあ右下は?

面倒なのでxmlです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"

android:gravity="right|bottom">

<Button android:text="TEST1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button android:text="TEST2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button android:text="TEST3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>


順番無視でbottom|right的に"|"区切りで指定すればいいです。

表示は以下