带有片段的标题栏与列表视图重叠



我是安卓系统的初学者。我有一个标题栏,它分别放在tool_bar.xml中。现在我有两个片段。从一个片段中,我创建了一个新的活动,其中必须包含tool_bar.xml(标题栏)。问题是当我在新创建的activty的xml文件中"包含"tool_bar.xml时。标题栏与列表视图重叠。

我也尝试过在On create方法中使用自定义标题栏方法,但没有成功。我认为包含tool_bar.xml是可行的,但它与列表视图重叠。

Tool_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@color/ColorPrimary"
android:elevation="2dp"
android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentTop="true"/>

Activty_category_blogs.xml(新建活动的xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="wrap_content" tools:context="com.example.talha.test_fragement.Category_Blogs"
xmlns:ads="http://schemas.android.com/apk/res-auto">
<include layout="@layout/tool_bar" />
<FrameLayout  android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ListView android:id="@android:id/list" android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adView" android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:layout_alignParentBottom="true"
        />

    <TextView android:id="@android:id/empty" android:layout_width="match_parent"
        android:layout_height="match_parent" android:gravity="center"
        android:layout_gravity="right|top" />
</FrameLayout>

编辑:

Oncreate方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category__blogs);
    Intent intent = getIntent();
    String message = intent.getStringExtra(Tab2.EXTRA_MESSAGE);
    String link = message + "?feed=titles";
    Log.d("ye category click krne par next activity me ye link bnta hy parsing ke liye",link);
    loadPage(link);
}

空对象引用的Log Cat错误:

2339-2339/com.example.talha.test_fragement E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.talha.test_fragement, PID: 2339
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.talha.test_fragement/com.example.talha.test_fragement.Category_Blogs}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
        at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:100)
        at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:93)
        at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:78)
        at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:204)
        at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
        at com.example.talha.test_fragement.Category_Blogs.onCreate(Category_Blogs.java:42)
        at android.app.Activity.performCreate(Activity.java:5937)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)

在活动的OnCreate()方法中,您应该为当前活动设置工具栏:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category__blogs);
    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar)
}

Activity_Category_Logs布局将包含以下内容:

<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="@color/ColorPrimary"
    android:elevation="2dp"
    android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_alignParentTop="true"/>
        <LinearLayout
            android:layout_marginTop="?attr/actionBarSize"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:minLines="1"
                android:id="@+id/textViewTitle" />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>

toolbar文件将具有在您的活动中使用的相同工具栏,而没有额外的控件:

<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="@color/ColorPrimary"
    android:elevation="2dp"
    android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_alignParentTop="true"/>

在您的片段OnCreate()方法中,您应该能够设置textViewTitle:的文本

((TextView)findViewById(R.id.textViewTitle)).setText("XYZ");
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="wrap_content" tools:context="com.example.talha.test_fragement.Category_Blogs"
xmlns:ads="http://schemas.android.com/apk/res-auto">
<include layout="@layout/tool_bar" android:id="@+id/toolbar" />
<RelativeLayout android:layout_width="fill_parent"
    android:layout_below="@+id/toolbar"
    android:layout_height="fill_parent">
    <ListView android:id="@android:id/list" android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/adView" android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:layout_alignParentBottom="false"
        />

    <TextView android:id="@android:id/empty" android:layout_width="match_parent"
        android:layout_height="match_parent" android:gravity="center"
        android:layout_gravity="right|top" />
</RelativeLayout >

最新更新