使用 <include> 标签时如何将 TextView 添加到工具栏?
How can I add TextView to Toolbar when I'm using the <include> tag?
所以,我有一个 toolbar.xml 文件:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"/>
而且,我的布局中有一个 <include...
标签:
<include
layout="@layout/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/toolbar"/>
所以我听说如果我想让 activity 的标题居中在 toolbar
上,我只需要在工具栏上添加一个 TextVIew
,就像这样:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbarLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<TextView .../>
</android.support.v7.widget.Toolbar>
但是,我使用的是 include
标签,但文本显然没有显示:
<include
layout="@layout/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/toolbar">
<TextView
android:text="@string/createNewOccasion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView5"
android:textSize="18sp"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
/>
</include>
那么,我该怎么做才能解决这个问题呢?这是它的显示方式:
如果你想看我完整的真实布局,去here。
这是我的 activity 如果它有帮助的话:http://pastebin.com/g6ZXAxe5
如果您想向 Toolbar
添加视图,则必须将其作为子项添加到 Toolbar
中,而不是 include 的子项。
您已经有了这段代码:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbarLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/toolbarTextView"
android:layout_width="20dp"
android:layout_height="match_parent"
android:visibility="gone"/>
</android.support.v7.widget.Toolbar>
如果你只想在一个 Activity
上显示这个 TextView
然后将 TextView
visibility
默认设置为 GONE
并将其更改为 VISIBLE
在您要显示的 Activity
内。
您可以在主布局中包含上述布局的任何 Activity
中获得 TextView
。
TextView toolbarTextView = (TextView) findViewById(R.id.toolbarTextView);
所以,我有一个 toolbar.xml 文件:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"/>
而且,我的布局中有一个 <include...
标签:
<include
layout="@layout/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/toolbar"/>
所以我听说如果我想让 activity 的标题居中在 toolbar
上,我只需要在工具栏上添加一个 TextVIew
,就像这样:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbarLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<TextView .../>
</android.support.v7.widget.Toolbar>
但是,我使用的是 include
标签,但文本显然没有显示:
<include
layout="@layout/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/toolbar">
<TextView
android:text="@string/createNewOccasion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView5"
android:textSize="18sp"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
/>
</include>
那么,我该怎么做才能解决这个问题呢?这是它的显示方式:
如果你想看我完整的真实布局,去here。 这是我的 activity 如果它有帮助的话:http://pastebin.com/g6ZXAxe5
如果您想向 Toolbar
添加视图,则必须将其作为子项添加到 Toolbar
中,而不是 include 的子项。
您已经有了这段代码:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbarLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/toolbarTextView"
android:layout_width="20dp"
android:layout_height="match_parent"
android:visibility="gone"/>
</android.support.v7.widget.Toolbar>
如果你只想在一个 Activity
上显示这个 TextView
然后将 TextView
visibility
默认设置为 GONE
并将其更改为 VISIBLE
在您要显示的 Activity
内。
您可以在主布局中包含上述布局的任何 Activity
中获得 TextView
。
TextView toolbarTextView = (TextView) findViewById(R.id.toolbarTextView);