工具栏下的屏​​幕上没有显示任何内容

时间:2015-09-11 12:23:53

标签: android android-layout android-fragments toolbar

我有Activiti。有一个工具栏和片段。但是在工具栏下,此片段未显示。记录正常,工作的一个片段,唯一的问题是只有显示器。
这是我的活动代码:

Toolbar toolbar;

public void initToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar1);
    setSupportActionBar(toolbar);
    setTitle(R.string.app_name);
    toolbar.setBackgroundResource(R.drawable.header);
    toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    initToolbar();
    // Create the AccountHeader
    DrawerClass.drawer(this, toolbar);
    ProfileFragment profileFragment = new ProfileFragment();
    FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction();
    transaction.add(R.id.profile_fragment_layout, profileFragment);
    transaction.commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_general, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

这和活动的布局:

<android.support.design.widget.CoordinatorLayout 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=".GeneralActivity"
android:background="@color/event_background">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/event_background"
    android:titleTextColor="@color/text_color"
    android:elevation="4dp"
    android:minHeight="?attr/actionBarSize"
    android:paddingTop="@dimen/tool_bar_top_padding"
    android:layout_marginTop="-25dp"
    android:transitionName="actionBar">

</android.support.v7.widget.Toolbar>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/profile_fragment_layout"
    android:layout_below="@+id/toolbar1">
</FrameLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    app:elevation="4dp"
    android:src="@drawable/pencil"
    app:layout_anchor="@id/toolbar1"
    app:layout_anchorGravity="bottom|right|end"
    app:borderWidth="0dp"/>

</android.support.design.widget.CoordinatorLayout>


如果您需要我的片段代码:

View view;
@Bind(R.id.dad_img)
CircleImageView dad_image;
@Bind(R.id.mum_img)
CircleImageView mum_image;

public void buildDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Диалог");
    builder.setMessage("Выберите действие");
    builder.setCancelable(true);
    builder.setPositiveButton("Сообщение", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss(); // Отпускает диалоговое окно
        }
    });
    builder.setNegativeButton("Перевод", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss(); // Отпускает диалоговое окно
        }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.profile_fragment, container, false);
    ButterKnife.bind(this, view);
    Log.d("FRAGMENT", "working");
    dad_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            buildDialog();
        }
    });
    mum_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            buildDialog();
        }
    });
    return view;
}

和片段的布局:

<?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">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/text_color">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_marginLeft="10dp"
                android:id="@+id/profile_school"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/study"
                    android:gravity="center"
                    android:layout_weight="1"/>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_weight="5">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="#000"
                        android:textSize="16sp"
                        android:paddingLeft="10dp"
                        android:text="@string/drawer_second_line"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="#000"
                        android:textSize="16sp"
                        android:paddingLeft="10dp"
                        android:layout_marginBottom="10dp"
                        android:text="@string/nickname" />
                </LinearLayout>
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:id="@+id/divider1"
                android:layout_below="@+id/profile_school"
                android:background="@android:color/darker_gray"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"
                android:layout_below="@+id/divider1"
                android:id="@+id/profile_phone"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/phone"
                    android:layout_marginTop="2dp"
                    android:gravity="center"
                    android:layout_weight="1"/>

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

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="#000"
                            android:textSize="16sp"
                            android:layout_margin="10dp"
                            android:layout_centerVertical="true"
                            android:text="@string/phone"/>
                    </RelativeLayout>
                </LinearLayout>
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/profile_phone"
                android:id="@+id/divider2"
                android:background="@android:color/darker_gray"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_marginLeft="10dp"
                android:layout_below="@+id/divider2"
                android:id="@+id/profile_parent"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/parents"
                    android:gravity="center"
                    android:layout_weight="1"/>

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

                        <de.hdodenhof.circleimageview.CircleImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/dad_img"
                            android:layout_marginLeft="10dp"
                            android:src="@mipmap/ic_launcher"/>

                        <de.hdodenhof.circleimageview.CircleImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/mum_img"
                            android:layout_marginLeft="10dp"
                            android:layout_below="@+id/dad_img"
                            android:layout_marginTop="5dp"
                            android:src="@mipmap/ic_launcher"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="#000"
                            android:textSize="16sp"
                            android:text="@string/mum_name"
                            android:id="@+id/textView2"
                            android:layout_marginBottom="15dp"
                            android:layout_marginLeft="10dp"
                            android:layout_alignBottom="@+id/dad_img"
                            android:layout_toRightOf="@+id/dad_img"
                            android:layout_toEndOf="@+id/dad_img" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="#000"
                            android:textSize="16sp"
                            android:text="@string/dad_name"
                            android:id="@+id/textView3"
                            android:layout_marginTop="15dp"
                            android:layout_marginLeft="10dp"
                            android:layout_alignTop="@+id/mum_img"
                            android:layout_toRightOf="@+id/mum_img"
                            android:layout_toEndOf="@+id/mum_img" />
                    </RelativeLayout>
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
</RelativeLayout>

请帮我找到我的错误:) 重复:当我打开我的活动和片段加载时,我在工具栏下看不到任何内容

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,我将相对布局添加到CoordinatorLayout:

<android.support.design.widget.CoordinatorLayout 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=".GeneralActivity"
    android:background="@color/event_background">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/event_background"
        android:titleTextColor="@color/text_color"
        android:minHeight="?attr/actionBarSize"
        android:paddingTop="@dimen/tool_bar_top_padding"
        android:layout_marginTop="-25dp"
        android:transitionName="actionBar1">

    </android.support.v7.widget.Toolbar>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/profile_fragment_layout"
        android:layout_below="@+id/toolbar1">
    </FrameLayout>
</RelativeLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    app:elevation="4dp"
    android:src="@drawable/pencil"
    app:layout_anchor="@id/toolbar1"
    app:layout_anchorGravity="bottom|right|end"
    app:borderWidth="0dp"/>

</android.support.design.widget.CoordinatorLayout>