是否可以在不使用
的情况下将图像添加到工具栏“ android.support.v7.widget.Toolbar”?
我已经使用“ android.support.constraint.ConstraintLayout”完成了该应用程序。
要在右上角添加徽标。 enter image description here
答案 0 :(得分:0)
创建工具栏布局
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和居中工具栏文本
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();
}
});
}
}
希望这对您有所帮助。