将图像添加到android中的工具栏 [英] Add an image to the toolbar in android

查看:91
本文介绍了将图像添加到android中的工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不使用

"android.support.v7.widget.Toolbar"?

"android.support.v7.widget.Toolbar"?

我已经使用"android.support.constraint.ConstraintLayout"完成了该应用程序.

i already finished the app using "android.support.constraint.ConstraintLayout".

要在右上角添加徽标. 在此处输入图片描述

want add the logo on top-right side. enter image description here

推荐答案

创建工具栏布局

app_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/app_color">


    <RelativeLayout
        android:id="@+id/rr_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:src="@mipmap/ic_launcher">

        <RelativeLayout
            android:id="@+id/rr_toolbar_left"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:src="@mipmap/ic_launcher">

            <ImageView
                android:id="@+id/iv_toolbar_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:padding="8dp"
                android:visibility="visible" />
        </RelativeLayout>

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="5dp"
            android:ellipsize="end"
            android:gravity="center"
            android:text="EXAMPLE"
            android:paddingRight="5dp"
            android:singleLine="true"
            android:textColor="@android:color/white"
            android:textSize="22sp" />

        <RelativeLayout
            android:id="@+id/rr_toolbar_right"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingLeft="10dp"
            android:paddingRight="20dp">

            <ImageView
                android:id="@+id/iv_toolbar_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:visibility="visible" />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_login_"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll_toolbar_header_signup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include layout="@layout/app_toolbar" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="@color/white"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/ll_main_fragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="8.5"
            android:orientation="vertical">

            <com.baoyz.widget.PullRefreshLayout
                android:id="@+id/swipeRefreshLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/adViewContainer">


                <android.support.v7.widget.RecyclerView
                    android:id="@+id/rvNewsList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:background="@color/white"
                    android:scrollbarSize="0dp"
                    android:scrollbars="vertical"
                    android:visibility="gone" />

            </com.baoyz.widget.PullRefreshLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

MainActivity.Java

像这样以编程方式设置backButton和居中工具栏文本

Set backButton and Center Toolbar Text Programetically like this

public class MainActivityextends Activity i {

    private ImageView iv_toolbar_left = null, iv_toolbar_right = null;
    private TextView toolbar_title = null;
    private RelativeLayout rr_toolbar_left = null, rr_toolbar_right = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.slid_in_right, R.anim.slid_out_left);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_edit_case);

        iv_toolbar_left = (ImageView) findViewById(R.id.iv_toolbar_left);
        iv_toolbar_left.setImageResource(R.mipmap.ic_back);
        iv_toolbar_left.setVisibility(View.VISIBLE);
        iv_toolbar_right = (ImageView) findViewById(R.id.iv_toolbar_right);
        toolbar_title = (TextView) findViewById(R.id.toolbar_title);
        toolbar_title.setText("Edit Status");
        rr_toolbar_left = (RelativeLayout) findViewById(R.id.rr_toolbar_left);
        rr_toolbar_right = (RelativeLayout) findViewById(R.id.rr_toolbar_right);
        rr_toolbar_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.slid_in_left, R.anim.slid_out_right);
                finish();
            }
        });

    }
}

希望这对您有所帮助.

这篇关于将图像添加到android中的工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆