很少包括在同一个空间

时间:2017-06-19 16:11:08

标签: android android-layout

正如您在image中看到的那样, profile_content 布局会占用整个空间,当我按下"个人资料"按钮正确显示信息。

但是当我按下"消息"按钮它不显示任何内容。你可以在image中找到"蓝色矩形"不像profile_content布局那样填充整个空间。

两个布局在 android:layout_widt 参数中具有相同的值(wrap_content)。

我有一些代码:

public void onClick(View v) {
        switch(v.getId()){
            case R.id.bProfile:
                Log.d(TAG, "onClick, buttonProfile pressed");
                //Hide previous layout
                activeLayout = "bProfile";
                profileContent.setVisibility(View.VISIBLE);
                //Access for extras passed in from login activity
                tUserName.setText(getIntent().getStringExtra("tProfileName"));
                break;
            case R.id.bMessages:
                Log.d(TAG, "onClick, buttonMessages pressed");
                profileContent.setVisibility(View.INVISIBLE);

                messagesContent.setVisibility(View.VISIBLE);
                activeLayout = "bMessages";
                test.setText("Test");
                break;

        }
    }

也许我应该使用FragmentLayout来实现这个特殊的功能?

提前致谢。

1 个答案:

答案 0 :(得分:1)

LinearLayout在这种情况下不会帮助你。尝试使用RelativeLayout,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/buttonsMenu"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="QWERTY" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="QWERTY" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ASDFG" />
    </LinearLayout>

    <RelativeLayout
        android:layout_toRightOf="@id/buttonsMenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/profile_content"
            layout="@layout/profile_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone" />

        <include
            android:id="@+id/profile_content"
            layout="@layout/messages_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone" />
    </RelativeLayout>
</RelativeLayout>