[英]how to add ImageButton in title bar
I am trying to add an ImageButton
at right side of title bar of activity in Android, but when I execute application it gives error Unfortunately app stopped
. 我正在尝试在Android活动标题栏的右侧添加一个
ImageButton
,但是当我执行应用程序时,它给出了错误, Unfortunately app stopped
。 Kindly help: 请帮助:
custom_title.xml custom_title.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.abc.myapplication.MainActivity">
<ImageButton
android:id="@+id/button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:focusable="true"
android:focusableInTouchMode="false"
android:src="@android:drawable/star_big_on"
android:background="@null" />
</RelativeLayout>
MainActivity.java MainActivity.java
package com.example.abc.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
}
}
在RelativeLayout中替换android:layout_height:
android:layout_height="wrap_content"
Follow these guides from the Android Developer site and you will be a master! 遵循Android开发者网站上的这些指南,您将成为高手! Adding the App Bar
添加应用栏
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.