我需要在选项菜单中显示布局。
menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_likes"
android:showAsAction="never"
android:actionLayout="@layout/likes_layout"/>
<item
android:id="@+id/settings"
android:showAsAction="never"
android:title="Settings"
/>
</menu>
shape_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:color="#080808" android:width="2dp"/>
<corners android:radius="5dp" />
<solid android:color="#85ea32"/>
</shape>
likes_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:id="@+id/likes"
android:linksClickable="false"
android:text="Likes"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:layout_gravity="center"
android:id="@+id/notif_count"
android:background="@drawable/shape_notification"
android:text="0"
android:textSize="16sp"
android:gravity="center"
android:padding="2dp"
android:singleLine="true"
android:layout_margin="5dp"
android:textColor="#fff"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
选项列表可见。设置项目可见但喜欢布局为空白。
是否有人为隐藏物品做过动作布局?请帮帮我
答案 0 :(得分:3)
将android:actionLayout
更改为app:actionLayout
,以便您的菜单项声明应为
<item
android:id="@+id/action_likes"
app:showAsAction="never"
app:actionLayout="@layout/likes_layout"/>
已更新:
您必须使用PopupWindow
创建自定义下拉菜单。需要实现以存档它的代码:
menu.xml文件
<menu 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"
tools:context="project.com.example.MainActivity"
>
<item
android:id="@+id/item1"
android:title="@string/action_settings"
android:icon="@drawable/ic_launcher"
app:actionViewClass="android.widget.ImageButton"
app:showAsAction="always">
<menu>
</menu>
</item>
</menu>
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
locButton = (ImageButton) menu.findItem(R.id.action_settings).getActionView();
locButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(locButton, -40, 18);
}
});
return true;
}
public PopupWindow popupDisplay()
{
final PopupWindow popupWindow = new PopupWindow(this);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listViewLayout, null);
ListView listView=(ListView)view.findViewById(R.id.list);
// populate listview with custom view which will be shown in drow down menu
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}
希望它有所帮助。
答案 1 :(得分:1)
可以使用 自定义弹出窗口 来完成。
<强> RES /菜单/ menu.xml文件强>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/more"
android:title="More Options"
android:icon="@drawable/ic_menu_moreoverflow_normal_holo_light"
app:actionLayout="@layout/moreover"
app:showAsAction="always">
<menu>
</menu>
</item>
</menu>
<强> RES /布局/ moreover.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dropDowmImageBtn"
android:src="@drawable/ic_menu_moreoverflow_normal_holo_light"/>
</LinearLayout>
<强> RES /布局/ drop_down_item_row.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff">
<TextView
android:id="@+id/dropDownItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:minWidth="32dp"
android:minHeight="32dp"
android:textColor="#000"
android:text=""
android:layout_weight="0.7"
android:singleLine="true"
android:gravity="center_vertical"
android:layout_margin="5dp"/>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dropDownItemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="32dp"
android:minHeight="32dp"
android:background="@drawable/round"
android:text=""
android:textSize="16sp"
android:textColor="@android:color/white"
android:gravity="center"
android:padding="2dp"
android:singleLine="true"
android:layout_weight="0.3"
android:layout_marginTop="2dp"
android:visibility="gone"/>
</LinearLayout>
<强> RES /抽拉/ round.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="#24bbe6" />
<stroke android:color="#fffefe" android:width="1dp"/>
</shape>
<强> DropDownItems.java 强>
public class DropDownItems {
String txt;
int value;
Boolean showValue;
public DropDownItems(String txt, int value, Boolean showValue) {
this.txt = txt;
this.value = value;
this.showValue = showValue;
}
public String getTxt() {
return txt;
}
public int getValue() {
return value;
}
public Boolean getShowValue() {
return showValue;
}
}
<强> MainActivity.java 强>
public class MainActivity extends AppCompatActivity {
PopupWindow popupWindow;
private ArrayList<DropDownItems> items = new ArrayList<DropDownItems>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
items.add(new DropDownItems("Settings", 0, false));
items.add(new DropDownItems("Likes", 10, true));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
ImageButton item1 = (ImageButton) menu.findItem(R.id.more).getActionView().findViewById(R.id.dropDowmImageBtn);
item1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopUpWindow(v);
}
});
return true;
}
private void showPopUpWindow(View v) {
DropDownAdapter adapter = new DropDownAdapter(v.getContext(), items);
popupWindow = new PopupWindow(this);
ListView listViewSort = new ListView(this);
listViewSort.setDivider(null);
listViewSort.setDividerHeight(0);
listViewSort.setAdapter(adapter);
popupWindow.setFocusable(true);
popupWindow.setWidth(400);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(listViewSort);
popupWindow.showAsDropDown(v, 0, 0);
}
public class DropDownAdapter extends BaseAdapter {
Context context;
ArrayList<DropDownItems> items;
public DropDownAdapter(Context context, ArrayList<DropDownItems> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public DropDownItems getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DropDownItems item = getItem(position);
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.drop_down_item_row, null);
}
final TextView itemText = (TextView) convertView.findViewById(R.id.dropDownItemName);
final Button itemImage = (Button) convertView.findViewById(R.id.dropDownItemImage);
itemText.setText(item.getTxt());
itemText.setTag(item.getTxt());
itemImage.setText(String.valueOf(item.getValue()));
itemImage.setTag(item.getTxt());
if(!item.getShowValue()) {
itemImage.setVisibility(View.GONE);
} else {
itemImage.setVisibility(View.VISIBLE);
}
itemText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked "+itemText.getTag(), Toast.LENGTH_LONG).show();
if(popupWindow!=null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
}
});
return convertView;
}
}
}
<强> RES /抽拉/ ic_menu_moreoverflow_normal_holo_light.png 强>