如何将图标移动到右上角?
How to move an icon to the upper-right hand side?
我正在尝试在 android studio 的右上角创建一个图标,该图标位于 claims 单词旁边。它应该如下图所示:
Claims1.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
fk = bundle.getLong("ab");
View claims = inflater.inflate(R.layout.claims, container, false);
mDrawerLayout = (DrawerLayout)claims.findViewById(R.id.drawer_layout);
android.support.v7.app.ActionBar myActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
ImageView imageView = new ImageView(myActionBar.getThemedContext());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.mipmap.create);
android.support.v7.app.ActionBar.LayoutParams layoutParams = new android.support.v7.app.ActionBar.LayoutParams(
android.support.v7.app.ActionBar.LayoutParams.WRAP_CONTENT,
android.support.v7.app.ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 40;
imageView.setLayoutParams(layoutParams);
myActionBar.setCustomView(imageView);
return claims;
}
claims.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@mipmap/create" />
</android.support.v4.widget.DrawerLayout>
然而我只能得到这个
有人可以帮忙吗?谢谢
1. 添加
setHasOptionsMenu(true);
在你的 Fragment
中的 OnCreate
方法中。
2. 使用您的菜单
在 \res\menu
下创建 my_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon="@drawable/YOURICONHERE"
android:id="@+id/YOURIDHERE"
android:orderInCategory="100"
android:title="YOURTITLE"
app:showAsAction="always" />
</menu>
3.Override
方法onCreateOptionsMenu
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
//Do what you want with menu - listeners etc.
}
我正在尝试在 android studio 的右上角创建一个图标,该图标位于 claims 单词旁边。它应该如下图所示:
Claims1.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
fk = bundle.getLong("ab");
View claims = inflater.inflate(R.layout.claims, container, false);
mDrawerLayout = (DrawerLayout)claims.findViewById(R.id.drawer_layout);
android.support.v7.app.ActionBar myActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
ImageView imageView = new ImageView(myActionBar.getThemedContext());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.mipmap.create);
android.support.v7.app.ActionBar.LayoutParams layoutParams = new android.support.v7.app.ActionBar.LayoutParams(
android.support.v7.app.ActionBar.LayoutParams.WRAP_CONTENT,
android.support.v7.app.ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 40;
imageView.setLayoutParams(layoutParams);
myActionBar.setCustomView(imageView);
return claims;
}
claims.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@mipmap/create" />
</android.support.v4.widget.DrawerLayout>
然而我只能得到这个
有人可以帮忙吗?谢谢
1. 添加
setHasOptionsMenu(true);
在你的 Fragment
中的 OnCreate
方法中。
2. 使用您的菜单
在\res\menu
下创建 my_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon="@drawable/YOURICONHERE"
android:id="@+id/YOURIDHERE"
android:orderInCategory="100"
android:title="YOURTITLE"
app:showAsAction="always" />
</menu>
3.Override
方法onCreateOptionsMenu
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
//Do what you want with menu - listeners etc.
}