リニアレイアウトの電卓アプリを開発しようと思っているのですが、Android開発に詳しくないので誰が書いたらいいのかわかりません...例えば3つのボタンがあるようにするにはどうすればいいですか?単一の行 (水平方向のスペース全体を占める) と、3 つのボタンを含む 2 番目の行?
例:
[ 7 8 9 ]
[ 4 5 6 ]
[ 1 2 3 ]
リニアレイアウトの電卓アプリを開発しようと思っているのですが、Android開発に詳しくないので誰が書いたらいいのかわかりません...例えば3つのボタンがあるようにするにはどうすればいいですか?単一の行 (水平方向のスペース全体を占める) と、3 つのボタンを含む 2 番目の行?
例:
[ 7 8 9 ]
[ 4 5 6 ]
[ 1 2 3 ]
LinearLayoutでは、「ネストされた重みがパフォーマンスに悪い」という警告が表示される可能性があるため、TableLayoutを使用することをお勧めします。すべてのスペースを埋めるには、これで多くのandroid:weightSumを使用する必要があります。以下のものを試してください。
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="3"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="3">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="7"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="8"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="9"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="3">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="4"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="5"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="6"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="3">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="3"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="2"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1"
/>
</TableRow>
</TableLayout>
GridLayoutの使用をお勧めします。(http://developer.android.com/reference/android/widget/GridLayout.html)
GridLayoutはAndroid4以降で利用できるため、この別の質問を確認して、古いバージョンとの互換性を追加することをお勧めします。
LinearLayout を使用しないでください。代わりに、RelativeLayout 内で RelativeLayouts を試してください。
TableLayout を使用することもできますが、RelativeLayout の方がさまざまな画面ジオメトリにより適していることがわかりました。
LinearLayout に固執したい場合は、おそらくこのようなものが必要になるでしょう。他の人が指摘したように、TableLayout はより堅牢な場合があります。LinearLayouts はとてもシンプルだと思います。あなたのアプリのシンプルさを考えると、画面上でアニメーションや何かをしていない限り、Android デバイスにストレスを与えるとは信じがたいです。また、電卓の他のコンポーネントをこのレイアウトに配置するのは非常に簡単なスペースが必要になると確信しています。別のレベルのネストを追加するか、重みを変更して、より小さな行に対応することができます。方程式/答えが表示される上部。
<?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="0dp" android:layout_weight="1"
android:orientation="horizontal">
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"
/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"
/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9"
/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="0dp" android:layout_weight="1"
android:orientation="horizontal">
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"
/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"
/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="0dp" android:layout_weight="1"
android:orientation="horizontal">
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
/>
<Button android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"
/>
</LinearLayout>
</LinearLayout>