如果不删除导航栏,则无法将片段与父活动对齐

时间:2015-04-27 12:52:27

标签: android android-fragments android-studio

我正在编写我帮助创建的iOS应用程序的Android版本。

我正在使用模板导航菜单,当拖出或按下按钮时,它会在顶部放置一个栏,在侧面放置一个菜单。这样可以正常工作,但片段带有边距,在条形图和布局之间留下空白。

我尝试删除边距,但是它们会阻止顶部的菜单显示。

我不确定要包含哪些代码,因此我已将home.xml(片段)activity_main.xml和相关的java文件包含在内。

home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginTop="70dp"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/tvNo"
    android:layout_gravity="left" />
</LinearLayout>

activity_main.xml中     

<!-- The main content view -->
<FrameLayout
    android:id="@+id/flContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- The ActionBar -->
    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </include>
</FrameLayout>

<!-- The navigation drawer -->
<ListView android:id="@+id/lvDrawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:paddingTop="24dp"
    android:divider="@android:color/darker_gray"
    android:dividerHeight="0dp"
    android:background="@android:color/background_light" />

</uk.co.mrgyro.cropcirclelocatorandroid.FragmentNavigationDrawer>

Home.java

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import uk.co.mrgyro.cropcirclelocatorandroid.R;

public class Home extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.home, container, false);

    return rootView;
  }
}

MainActivity.java

package uk.co.mrgyro.cropcirclelocatorandroid;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity {
private FragmentNavigationDrawer dlDrawer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set a ToolBar to replace the ActionBar.
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Find our drawer view
    dlDrawer = (FragmentNavigationDrawer) findViewById(R.id.drawer_layout);
    // Setup drawer view
    dlDrawer.setupDrawerConfiguration((ListView) findViewById(R.id.lvDrawer), toolbar,
            R.layout.drawer_nav_item, R.id.flContent);
    // Add nav items
    dlDrawer.addNavItem("First", R.drawable.ic_one, "First Fragment", Home.class);
    dlDrawer.addNavItem("Second", R.drawable.ic_two, "Second Fragment", SecondFragment.class);
    dlDrawer.addNavItem("Third", R.drawable.ic_three, "Third Fragment", ThirdFragment.class);
    // Select default
    if (savedInstanceState == null) {
        dlDrawer.selectDrawerItem(0);
    }
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // If the nav drawer is open, hide action items related to the content
    if (dlDrawer.isDrawerOpen()) {
        // Uncomment to hide menu items
        // menu.findItem(R.id.mi_test).setVisible(false);
    }
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    // Uncomment to inflate menu items to Action Bar
    // inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.
    // ActionBarDrawerToggle will take care of this.
    if (dlDrawer.getDrawerToggle().onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    dlDrawer.getDrawerToggle().syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggles
    dlDrawer.getDrawerToggle().onConfigurationChanged(newConfig);
}

}

这是添加边距的方式 Before

这是将home.xml中的两个边距设置为0

的样子

After

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>
</resources>

我想要的外观就像第一张图片一样,除了没有空格。

提前谢谢

2 个答案:

答案 0 :(得分:1)

我不太了解您的活动设计和布局。如果你需要一个导航抽屉和一个操作栏,我只需要创建一个扩展ActionBarActivity的活动和写在主xml中的导航抽屉。然后在导航抽屉的内容侧放置您的活动内容。在代码中检索操作栏和抽屉并根据需要自定义它们。你可能不会有边缘等问题,而且会更干净。

答案 1 :(得分:0)

删除

android:layout_marginTop="70dp"
android:layout_marginLeft="20dp"

来自Home布局,

android:fitsSystemWindows="true"

来自您的活动布局。