Как установить параметры макета для относительного макета? - PullRequest
0 голосов
/ 08 февраля 2019

Я использую Android Studio для создания приложения.Пользователь выберет кнопку из списка из трех кнопок, которая переведет их в новую активность с именем SurveyActivity.В SurveyActivity есть несколько относительных макетов с контентом.Но в зависимости от того, какую кнопку нажать, появятся не все соответствующие макеты.Относительные макеты связаны друг с другом, потому что они находятся под другим

Проблема, с которой я столкнулся, заключается в том, что, когда определенные виды являются View.GONE, вы не можете получить доступ к идентификатору для Android: layout_below

Я уже пробовал этот пост но я не понимаю, почему вы используете параметры макета.

Я знаю, что если вы используете LinearLayout, вы можете это сделать, но я хотел знать, есть ли способ сделать это таким образом с помощью Relative Layouts.

Вот SurveyActivity:

public class SurveyActivity extends AppCompatActivity {

String timeOfDay;

private RelativeLayout relativeLayoutMentally, relativeLayoutPhysically, relativeLayoutEnergised,
        relativeLayoutMotivated, relativeLayoutAlert, relativeLayoutSatisfied, relativeLayoutCalm;



@Override
@TargetApi(17)
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_survey);
    // Initalisation

    relativeLayoutMentally = (RelativeLayout)findViewById(R.id.mentally);
    relativeLayoutPhysically = (RelativeLayout)findViewById(R.id.physically);
    relativeLayoutEnergised = (RelativeLayout)findViewById(R.id.energised);
    relativeLayoutMotivated = (RelativeLayout)findViewById(R.id.motivated);
    relativeLayoutAlert = (RelativeLayout)findViewById(R.id.alert);
    relativeLayoutSatisfied = (RelativeLayout)findViewById(R.id.satisfied);
    relativeLayoutCalm = (RelativeLayout)findViewById(R.id.calm);

    relativeLayoutMentally.setVisibility(View.VISIBLE);
    relativeLayoutPhysically.setVisibility(View.VISIBLE);
    relativeLayoutSatisfied.setVisibility(View.VISIBLE);

    // Intent

    Intent intent = getIntent();
    timeOfDay = intent.getStringExtra("Time of Day");


    switch(timeOfDay){
        case "Morning":



            relativeLayoutEnergised.setVisibility(View.VISIBLE);
        relativeLayoutMotivated.setVisibility(View.VISIBLE);
        relativeLayoutAlert.setVisibility(View.VISIBLE);
        relativeLayoutCalm.setVisibility(View.GONE);

            break;
        case "Afternoon":

            relativeLayoutEnergised.setVisibility(View.VISIBLE);
            relativeLayoutMotivated.setVisibility(View.VISIBLE);
            relativeLayoutAlert.setVisibility(View.VISIBLE);
            relativeLayoutCalm.setVisibility(View.GONE);

            break;
        case "Evening":

            relativeLayoutEnergised.setVisibility(View.GONE);
            relativeLayoutMotivated.setVisibility(View.GONE);
            relativeLayoutAlert.setVisibility(View.GONE);
            relativeLayoutCalm.setVisibility(View.VISIBLE);

            break;

    }

}
}

Вот XML-файл опроса:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.happinesssurvey.SurveyActivity">



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

            <RelativeLayout
                android:id="@+id/mentally"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <SeekBar
                    android:id="@+id/seekBarMentally"
                    style="@style/Widget.AppCompat.SeekBar.Discrete"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/questionMentally"
                    android:layout_marginTop="23dp"
                    android:max="10"
                    android:padding="10dp"
                    android:progress="5" />

                <TextView
                    android:id="@+id/questionMentally"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question_mentally" />

                <TextView
                    android:id="@+id/label_mentally"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""/>


            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/physically"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <!--android:layout_below="@+id/mentally"-->

                <SeekBar
                    android:id="@+id/seekBarPhysically"
                    style="@style/Widget.AppCompat.SeekBar.Discrete"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/questionPhysically"
                    android:layout_marginTop="23dp"
                    android:max="10"
                    android:padding="10dp"
                    android:progress="5" />

                <TextView
                    android:id="@+id/questionPhysically"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question_physically" />

                <TextView
                    android:id="@+id/label_physically"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""/>


            </RelativeLayout>


        <RelativeLayout
            android:id="@+id/energised"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <!--android:layout_below="@id/physically">-->

            <SeekBar
                android:id="@+id/seekBarEnergised"
                style="@style/Widget.AppCompat.SeekBar.Discrete"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/questionEnergised"
                android:layout_marginTop="23dp"
                android:max="10"
                android:padding="20dp"
                android:progress="5" />

            <TextView
                android:id="@+id/questionEnergised"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/question_energised" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_below="@+id/questionEnergised"
                android:gravity="fill_horizontal">

                <TextView
                    android:layout_width="90dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginEnd="220dp"
                    android:layout_marginRight="220dp"
                    android:text="@string/label_unenergised" />

                <TextView
                    android:layout_width="90dp"
                    android:layout_height="wrap_content"
                    android:text="@string/label_energised" />

            </LinearLayout>


        </RelativeLayout>



        <RelativeLayout
            android:id="@+id/motivated"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <!--android:layout_below="@id/energised">-->

            <SeekBar
                android:id="@+id/seekBarMotivated"
                style="@style/Widget.AppCompat.SeekBar.Discrete"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/questionMotivated"
                android:layout_marginTop="23dp"
                android:max="10"
                android:padding="20dp"
                android:progress="5" />

            <TextView
                android:id="@+id/questionMotivated"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/question_motivated" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_below="@+id/questionMotivated"
                android:gravity="fill_horizontal">

                <TextView
                    android:layout_width="90dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="220dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginEnd="220dp"
                    android:text="@string/label_unmotivated" />

                <TextView
                    android:layout_width="90dp"
                    android:layout_height="wrap_content"
                    android:text="@string/label_motivated" />

            </LinearLayout>


        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/satisfied"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <!--android:layout_below="@id/motivated">-->

            <SeekBar
                android:id="@+id/seekBarSatisfied"
                style="@style/Widget.AppCompat.SeekBar.Discrete"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/questionSatisifed"
                android:layout_marginTop="23dp"
                android:max="10"
                android:padding="20dp"
                android:progress="5" />

            <TextView
                android:id="@+id/questionSatisifed"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/question_satisifed" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_below="@+id/questionSatisifed"
                android:gravity="fill_horizontal">

                <TextView
                    android:layout_width="90dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="220dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginEnd="220dp"
                    android:text="@string/label_unsatisfied" />

                <TextView
                    android:layout_width="90dp"
                    android:layout_height="wrap_content"
                    android:text="@string/label_satisfied" />

            </LinearLayout>


        </RelativeLayout>



        <RelativeLayout
            android:id="@+id/alert"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <!--android:layout_below="@id/satisfied">-->

            <SeekBar
                android:id="@+id/seekBarAlert"
                style="@style/Widget.AppCompat.SeekBar.Discrete"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/questionAlert"
                android:layout_marginTop="23dp"
                android:max="10"
                android:padding="20dp"
                android:progress="5" />

            <TextView
                android:id="@+id/questionAlert"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/question_alert" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_below="@+id/questionAlert"
                android:gravity="fill_horizontal">

                <TextView
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="220dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginEnd="220dp"
                    android:text="@string/label_not_alert" />

                <TextView
                    android:layout_width="90dp"
                    android:layout_height="wrap_content"
                    android:text="@string/label_alert" />

            </LinearLayout>


        </RelativeLayout>

            <RelativeLayout
                android:id="@+id/calm"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <!--android:layout_below="@id/alert">-->

                <SeekBar
                    android:id="@+id/seekBarCalm"
                    style="@style/Widget.AppCompat.SeekBar.Discrete"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/questionCalm"
                    android:layout_marginTop="23dp"
                    android:max="10"
                    android:padding="20dp"
                    android:progress="5" />

                <TextView
                    android:id="@+id/questionCalm"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/question_calm" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:layout_below="@+id/questionCalm"
                    android:gravity="fill_horizontal">

                    <TextView
                        android:layout_width="80dp"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="220dp"
                        android:layout_marginStart="10dp"
                        android:layout_marginEnd="220dp"
                        android:text="@string/label_not_calm" />

                    <TextView
                        android:layout_width="90dp"
                        android:layout_height="wrap_content"
                        android:text="@string/label_calm" />

                </LinearLayout>


            </RelativeLayout>

        </LinearLayout>

</ScrollView>

Я ожидаю, что после того, как я это сделаю, появятся некоторые относительные макеты

Спасибо

1 Ответ

0 голосов
/ 08 февраля 2019

Если вы хотите установить layout_below программно, см. этот пост

Относительно вашего случая: layout_below не будет работать под LinearLayout в качестве родителя.

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

        <RelativeLayout
            android:id="@+id/mentally"
            ... >
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/physically"
            android:layout_below="@+id/mentally"
            ... >

Просто удалите layout_below и закажите в нужной вам последовательности.Если вы хотите изменить последовательность, то взгляните на: изменение порядка представлений в линейном макете android

...