在某个片段中添加工具栏

时间:2017-04-02 06:01:38

标签: android android-layout android-fragments

我有一项主要活动:

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

此活动只有一个framelayout,并使用此布局添加,替换和删除片段。

我有2个片段:

  • LoginFragment(constraintLayout)

    <android.support.constraint.ConstraintLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4CF">
    
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to Register"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.500" />
    

    • RegisterFragment(constraintLayout)

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="0dp"
        android:layout_height="56dp"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        tools:layout_editor_absoluteY="1dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to the Register fragment"
        tools:layout_editor_absoluteY="205dp"
        tools:layout_editor_absoluteX="62dp" />
    

登录片段首先显示。这应该没有动作栏。此片段中有一个按钮,它将使用寄存器片段替换此登录片段。在这个片段中,我希望操作栏显示出来,以便它可以在操作栏中使用后退箭头返回登录视图。我不知道在哪里将工具栏小部件放在代码中?我试图在寄存器片段中设置它,但它仍然显示奇怪。

3 个答案:

答案 0 :(得分:0)

我已经弄清楚了。在活动中,我只需将所有内容包装成线性布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

然后在片段中我有一个隐藏/显示工具栏的方法。

答案 1 :(得分:0)

Toobar移除RegisterFragment并将其放入activity_main.xml文件。

?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <FrameLayout 
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

以编程方式显示和隐藏Toolbar片段的OnCreate()方法。使用YOUR_ActionBar.show()显示ActionbarYOUR_ActionBar.hide()隐藏Actionbar

LoginFragment

// ActionBar
ActionBar mActionBar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ActionBar
    mActionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
    mActionBar.hide();

}

RegisterFragment

// ActionBar
ActionBar mActionBar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // ActionBar
    mActionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
    mActionBar.show();

}

答案 2 :(得分:0)

只需制作布局

toolbar.xml

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorAccent"
    android:elevation="3dp"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

只要您需要工具栏,只需包含类似

的布局
<include layout="@layout/toolbar.xml">