Android アプリケーションのログイン レイアウトで同じ行に 2 つのボタンを配置するにはどうすればよいですか?
8 に答える
線形レイアウトを作成するだけです。向きを水平に設定し、2 つのボタンを追加します。これで、必要なものが得られます。そのような質問を投稿する前に、グーグルで検索してみてください。確実に答えが得られます。このコードが役に立ちます。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
2 つのボタンをレイアウトに合わせたい場合は、両方のボタンの重みを 1 にします。
最善の解決策は、LinearLayout に2 つのボタン (幅が等しい)を配置することです。
もう1つ、同じ「幅」のボタンが必要な場合は、幅が0dpで、すべてのボタンに同じ重みのボタンを使用します。
そして、同じ「高さ」ボタンが必要な場合は、高さが0dpで、すべてのボタンに同じ重みを持つボタンを使用します。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
これを使用して、同じ行に2つのボタンを配置します....
<?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"
>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_alignParentBottom="true"
/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_toRightOf="@+id/login"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
Linear Layout(Horizontal) を追加する必要があります。次に、1行に多くのボタンを追加できます....
これには相対レイアウトを使用することもできます。
ここにあなたのためのコードがあります...
<?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">
<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content">
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
水平方向の1つの線形レイアウトを使用して、2つのボタンを追加できます
<LinearLayout
<Button1.../>
<Button2.../>
</LinearLayout>
RelativeLayout を使用する必要があると思います。次のようなことができます。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="fill_parent">
<Button
android:text="@+id/Button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true">
</Button>
<Button
android:text="@+id/Button02"
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true">
</Button>
</RelativeLayout>
また、こちらも参考にしてください。 http://www.mkyong.com/android/android-relativelayout-example/
これがあなたを助けることを願っています。
LinearLayout 内にボタンを配置する場合は、Orientation の値を「Vertical」に指定すると、ボタンが同じ行に自動的に配置されます。RelativeLayout を使用している場合は、1 つのボタンに対して android:layout_toLeftOf または android:layout_toRightOf を使用し、他のボタンの ID として値を指定します。正解した場合は、回答としてマークしてください。ありがとう...