工具栏背景图像大小

时间:2018-03-07 06:26:43

标签: android android-imageview toolbar

我在Android应用程序中工作。实际上我必须设置一个图像来代替工具栏。但我不知道该图像的确切大小。 我不是在询问行动栏大小。我google了很多但无法找到解决方案。

这是我的要求

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以创建自定义工具栏并将其添加到您的活动

<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:theme="@style/AppTheme.AppBarOverlay"
xmlns:android="http://schemas.android.com/apk/res/android">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <ImageView
            android:id="@+id/ivUser"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@drawable/ic_profile"
            android:layout_centerVertical="true"
            />

        <ImageView
            android:id="@+id/tvTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_logo_light"
            android:layout_centerInParent="true"
            android:padding="15dp"
            />

        <ImageView
            android:id="@+id/ivLogout"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:background="@drawable/ic_logout"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_margin="10dp"
            android:visibility="gone"
            />

    </RelativeLayout>

</android.support.v7.widget.Toolbar>

您可以将此工具栏xml添加到您的活动

<include
     layout="@layout/toolbar.xml">

在活动中使用此工具栏,使用活动中的以下代码

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView tvTitle = (TextView) toolbar.findViewById(R.id.tvTitle);
ImageView ivUser = (ImageView) toolbar.findViewById(R.id.ivUser);
ImageView ivLogout = (ImageView) toolbar.findViewById(R.id.ivLogout);
getSupportActionBar().setDisplayShowTitleEnabled(false);

注意:对活动使用NoActionBar主题。禁用默认工具栏

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

答案 1 :(得分:0)

请在xml文件中尝试此代码;

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.kolektif_merta.testapp.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/layout_home_appBarLayout_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="56dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize">

        </android.support.v7.widget.Toolbar>

    </RelativeLayout>

    </android.support.design.widget.AppBarLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</android.support.constraint.ConstraintLayout>           

在onCreate方法中的java类中(假设你在活动中);

    toolbar = findViewById(R.id.toolbar);
    toolbar.setBackground(ContextCompat.getDrawable(this,R.drawable.test));

根据您的要求,测试绘图是1200x640。 另外,请不要忘记更新styles.xml文件:

  <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

结果如下: custom toolbar background witm image

相关问题