如何设置相对布局的布局参数?

时间:2019-02-08 13:15:56

标签: android android-relativelayout android-layoutparams

我正在使用Android Studio创建应用。用户将从三个按钮的列表中选择一个按钮,这会将它们带到名为SurveyActivity的新活动。在SurveyActivity中,有一些带有内容的相对布局。但是,取决于要按下的按钮,并不是所有的相对布局都会出现。相对布局彼此链接,因为它们是一个接一个的。

我遇到的问题是,当某些视图为View.GONE时,您将无法访问android:layout_below的ID

我已经尝试过this post,但我似乎不明白您为什么使用布局参数。

我知道如果您使用LinearLayout可以做到,但是我想知道相对布局是否可以通过这种方式实现。

这是SurveyActivity:

funcWrapper

这是调查的xml文件:

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;

    }

}
}

我希望这样做后,某些相对布局会显示

谢谢

1 个答案:

答案 0 :(得分:0)

如果要以编程方式在下面设置layout_below,请参见this post

关于您的情况: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并按所需顺序对其进行排序。如果要更改顺序,请查看: change order of views in linear layout android