【问题标题】:Title Bar with Fragment overlap with list view带有片段的标题栏与列表视图重叠
【发布时间】:2016-03-11 03:54:12
【问题描述】:

我是 Android 的初学者。我有一个单独放置在 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>

编辑:

创建方法:

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() 方法吗?
  • 请检查,我已编辑
  • 好的,你有 RelativeLayout 作为基本布局。因此,将您的框架布局属性 android:layout_below 与您的工具栏的 id 一起放入。
  • 我已经像这样编辑了它,但它仍然重叠:

标签: android xml android-fragments layout android-listview


【解决方案1】:

在您的活动的 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_Blogs 布局将包含以下内容:

<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>

tool_bar 文件将与您的活动中使用的工具栏相同,但没有额外的控件:

<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");

【讨论】:

  • 它在 setSupportActionBar 的无法解析方法上给出错误
  • 你的activity也需要像这样扩展AppCompatActivity:public class MainActivity extends AppCompatActivity
  • 它给出了空对象引用。我已经粘贴了日志猫错误
  • 您收到空对象引用错误,因为您在 SetTitle() 之前尝试 GetTitle()
  • 你能告诉在哪里使用 setTitle()
【解决方案2】:
<?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 >

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    相关资源
    最近更新 更多