我正在使用 Android Studio 创建一个应用程序。用户将从三个按钮的列表中选择一个按钮,这会将他们带到一个名为 SurveyActivity 的新 Activity。在 SurveyActivity 中,有一些与内容相关的布局。但根据按下哪个按钮,并非所有相关布局都会出现。相对布局相互链接,因为它们在另一个之下。
我遇到的问题是,当某些视图是 View.GONE 时,您无法访问 android:layout_below 的 ID
我已经尝试过这篇文章,但我似乎不明白你为什么使用布局参数。
我知道如果你使用 LinearLayout 你可以做到,但我想知道是否有办法用相对布局做到这一点。
这是调查活动:
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>
我希望在我这样做之后,一些相关的布局会出现
谢谢