gpt4 book ai didi

android - 在工具栏右侧添加自定义 View

转载 作者:IT老高 更新时间:2023-10-28 23:08:44 36 4
gpt4 key购买 nike

我正在努力实现这一点(toolbar 中带有红色背景的计时器):

A timer in the toolbar along with red background

我正在尝试在 toolbar 中添加 customView。它总是在后箭头旁边的最左边结束。就像下图中的文本 YP 一样,是添加到工具栏的自定义 Textviewenter image description here .

工具栏的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="@+id/base_activity_toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

添加布局的代码:

    LayoutInflater mInflater= LayoutInflater.from(context);
View mCustomView = mInflater.inflate(R.layout.textview, null);
toolbar.addView(mCustomView);

这是我现在添加的用于测试的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:gravity="end"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:text="yp"
android:layout_height="wrap_content"/>
</LinearLayout>

我主要遗漏了一些东西,无法弄清楚。拔掉我的头发。

最佳答案

我刚刚将 layout_gravity 设置为 right并且它起作用了

  <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?android:actionBarSize"
app:title="NEW REQUESTS"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blahblah"
android:layout_gravity="right"
/>
</android.support.v7.widget.Toolbar>

关于android - 在工具栏右侧添加自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33909621/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com