Android Kotlin - 在工具栏内设置徽标
Android Kotlin - set logo inside toolbar
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.NoActionBar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/logo"
android:layout_width="108dp"
android:layout_height="36dp"
app:srcCompat="@drawable/logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|end"
android:orientation="horizontal"
android:paddingEnd="10dp">
<ImageView
android:id="@+id/btnOpenSearch"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="10dp"
app:tint="@color/colorText"
app:srcCompat="@drawable/ic_search" />
<ImageView
android:id="@+id/btnProfileImage"
android:layout_width="42dp"
android:layout_height="42dp"
android:adjustViewBounds="true"
android:padding="6dp"
app:layout_constraintBottom_toTopOf="@+id/MenuNickname"
app:srcCompat="@drawable/ic_user" />
<TextView
android:id="@+id/btnSignIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:padding="10dp"
android:text="@string/sign_in"
android:textColor="@color/colorText"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
徽标未显示在工具栏内,但右侧的图标显示
我试过了:toolbar.setLogo(R.drawable.logo)
但是不仅不行而且右边的图标也消失了
我也在用 ActionBarDrawerToggle
:
toolbar = findViewById(R.id.toolbar)
drawerLayout = findViewById(R.id.drawer_layout)
navView = findViewById(R.id.nav_view)
val toggle = ActionBarDrawerToggle(
this, drawerLayout, toolbar, 0, 0
)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
navView.setNavigationItemSelectedListener(this)
解决方案是使用 src
方法而不是 srcCompat
:
<ImageView
android:id="@+id/logo"
android:layout_width="108dp"
android:layout_height="36dp"
android:src="@drawable/logo" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.NoActionBar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/logo"
android:layout_width="108dp"
android:layout_height="36dp"
app:srcCompat="@drawable/logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|end"
android:orientation="horizontal"
android:paddingEnd="10dp">
<ImageView
android:id="@+id/btnOpenSearch"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="10dp"
app:tint="@color/colorText"
app:srcCompat="@drawable/ic_search" />
<ImageView
android:id="@+id/btnProfileImage"
android:layout_width="42dp"
android:layout_height="42dp"
android:adjustViewBounds="true"
android:padding="6dp"
app:layout_constraintBottom_toTopOf="@+id/MenuNickname"
app:srcCompat="@drawable/ic_user" />
<TextView
android:id="@+id/btnSignIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:padding="10dp"
android:text="@string/sign_in"
android:textColor="@color/colorText"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
徽标未显示在工具栏内,但右侧的图标显示
我试过了:toolbar.setLogo(R.drawable.logo)
但是不仅不行而且右边的图标也消失了
我也在用 ActionBarDrawerToggle
:
toolbar = findViewById(R.id.toolbar)
drawerLayout = findViewById(R.id.drawer_layout)
navView = findViewById(R.id.nav_view)
val toggle = ActionBarDrawerToggle(
this, drawerLayout, toolbar, 0, 0
)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
navView.setNavigationItemSelectedListener(this)
解决方案是使用 src
方法而不是 srcCompat
:
<ImageView
android:id="@+id/logo"
android:layout_width="108dp"
android:layout_height="36dp"
android:src="@drawable/logo" />