片段在MainActivity的按钮下

时间:2016-08-03 16:13:35

标签: android android-fragments android-activity

我是Android编程的新手,我有一个问题: 我有一个"主页"一些按钮在哪里。如果我用Fragment替换RelativeLayout是片段列表上的按钮。在我开始片段后,如何使按钮不可见? Here is a Screenshot. All yellow marked are from the MainActivity and the list behind them is from the fragment. [LINK]

重要的代码:

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_functions) {
        startFragment(FunctionsFragment.newInstance(1), "TagFunctions");
        Log.d("SMSAT", "nav_functions pressed");
    } else if (id == R.id.nav_macro) {

    } else if (id == R.id.nav_settings) {

    } else if (id == R.id.nav_platzhalter) {

    } else if (id == R.id.nav_contact) {

    } else if (id == R.id.nav_share) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

private void startFragment(Fragment fragment, String tag){


    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.main_fragment_container, fragment, tag).commit();

    Log.d("SMSAT", "startFragment() ausgeführt");

}

?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="de.mika.smsantitheft.MainActivity"
tools:showIn="@layout/app_bar_main"
android:id="@+id/main_relative_layout">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/home_sms_commands"
    android:id="@+id/textViewSmsBefehleStatus"
    android:layout_marginTop="33dp"
    android:layout_marginLeft="27dp"
    android:layout_marginStart="27dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

  <ToggleButton
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:text="New ToggleButton"
    android:id="@+id/toggleButton"
    android:textOn="@string/aktivated"
    android:textOff="@string/deaktivated"
    android:layout_below="@+id/textViewSmsBefehleStatus"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="30dp"
    android:layout_marginEnd="30dp" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/menu_functions"
    android:id="@+id/button"
    android:layout_below="@+id/toggleButton"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="49dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/menu_macros"
    android:id="@+id/button2"
    android:layout_below="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/menu_settings"
    android:id="@+id/button3"
    android:layout_below="@+id/button2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/menu_contact"
    android:id="@+id/button5"
    android:layout_below="@+id/button3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/menu_recommend"
    android:id="@+id/button4"
    android:layout_below="@+id/button5"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

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

   </RelativeLayout>

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

您是否尝试过在startFragment中设置隐藏按钮?

Button button = (Button) findViewById(R.id.button_location);
button.setVisibility(View.GONE);

答案 1 :(得分:0)

将所有按钮放入布局中。改变整个布局的可见性。

答案 2 :(得分:0)

尝试使用add方法而不是替换它。

如果这没有帮助,请尝试将您的appliaction主题设置为android:Theme.Material.Light。

如果这没有帮助,请在XML上将新片段的背景设置为白色。这会让你觉得你的笔记本上还有另一张纸。