0

私はAndroid Programming: The Big Nerd Ranch Guide book をフォローしていますが、非常に初歩的な質問があります。

GeoQuiz という名前の最初のアプリで、右ボタンを画面の右端に移動する方法 (ボタンのサイズを に維持する方法wrap_content):

スクリーンショット

私のレイアウト ファイルは、GitHub でactivity_quiz.xmlとして見ることができます。抜粋は次のとおりです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="de.afarber.geoquiz.QuizActivity" >
....
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button" />

        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button" />

    </LinearLayout>
....       
</LinearLayout>
4

4 に答える 4

2

これを試してください

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.afarber.geoquiz.QuizActivity" >
....
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/true_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/true_button" />

    <Button
        android:id="@+id/false_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/false_button" />

</RelativeLayout>
....       
</LinearLayout>
于 2014-09-10T11:00:39.373 に答える
1

この方法を試してみてください。これが問題の解決に役立つことを願っています。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="de.afarber.geoquiz.QuizActivity" >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button" />
        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button" />

    </LinearLayout>
</LinearLayout>
于 2014-09-10T11:05:27.020 に答える
-1

以前の水平レイアウト内にさらに 2 つの水平レイアウトを作成し、それぞれの重みを 0.5 にすることをお勧めします。次に、水平レイアウトの中央に true ボタンと false ボタンを配置できます。コードは次のとおりです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"

android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.afarber.geoquiz.QuizActivity" >

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >


    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center" >

   <Button
       android:id="@+id/true_button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/true_button" />

       </LinearLayout>

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="0dp"
    android:layout_height="wrap_content" 
    android:layout_weight="0.5"
    android:gravity="center">

        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button"/>

        </LinearLayout>
</LinearLayout>

ボタンのサイズが wrap_content のままで、ボタンを広げることができるので、これが役立つことを願っています。相対レイアウトを使用して問題を簡単に解決できますが、画面の解像度が変更されたときに問題が発生する可能性があるため、相対レイアウトを使用しないことを強くお勧めします

于 2014-09-10T11:36:56.933 に答える