gpt4 book ai didi

android - 如何在 GridView 中显示菜单项

转载 作者:太空狗 更新时间:2023-10-29 15:41:24 26 4
gpt4 key购买 nike

如何在操作栏的 GridView 中显示菜单项。默认情况下,它以 listvew 类型显示,但如何在 gridview 中显示。对于文件附件选项,我想在 GridView 中显示操作菜单项,如 wats app。谁能告诉我如何在 android 中点击操作菜单项显示自定义 View

enter image description here

最佳答案

试试这个,它工作正常。

创建 custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp" >

<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/image"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/image1"
android:text="Menu1" />
</RelativeLayout>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp" >

<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/image"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/image2"
android:text="Menu2" />
</RelativeLayout>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp" >

<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/image"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/image3"
android:text="Menu3" />
</RelativeLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/layout2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp" >

<ImageView
android:id="@+id/image4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/image"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/image4"
android:text="Menu4" />
</RelativeLayout>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp" >

<ImageView
android:id="@+id/image5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/image"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/image5"
android:text="Menu5" />
</RelativeLayout>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp" >

<ImageView
android:id="@+id/image6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/image"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/text6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/image6"
android:text="Menu6" />
</RelativeLayout>
</LinearLayout>

像这样创建布局或创建自定义 GridView 。

创建文件 res/menu/main.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="com.example.customactionbar.MainActivity" >

<item
android:id="@+id/menus"
android:icon="@drawable/ic_launcher"
android:orderInCategory="100"
android:title="@string/menu"
app:showAsAction="always"/>
</menu>

创建文件 res/anim/grow_from_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0.3" android:toXScale="1.0"
android:fromYScale="0.3" android:toYScale="1.0"
android:pivotX="50%" android:pivotY="0%"
android:duration="@android:integer/config_shortAnimTime"
/>
<alpha
android:interpolator="@android:anim/decelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_shortAnimTime"
/>
</set>

创建文件 res/anim/shrink_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0" android:toXScale="0.3"
android:fromYScale="1.0" android:toYScale="0.3"
android:pivotX="50%" android:pivotY="0%"
android:duration="@android:integer/config_shortAnimTime"
/>
<alpha
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_shortAnimTime"
/>
</set>

在 style.xml 中粘贴以下行

 <style name="Animations" />

<style name="Animations.GrowFromTop">
<item name="@android:windowEnterAnimation">@anim/grow_from_top</item>
<item name="@android:windowExitAnimation">@anim/shrink_from_bottom</item>
</style>

创建类文件 PopupHelper.java

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;

public class PopupHelper {

public static PopupWindow newBasicPopupWindow(Context context) {
final PopupWindow window = new PopupWindow(context);

// when a touch even happens outside of the window
// make the window go away
window.setTouchInterceptor(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
window.dismiss();
return true;
}
return false;
}
});

window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
window.setTouchable(true);
window.setFocusable(true);
window.setOutsideTouchable(true);

window.setBackgroundDrawable(new BitmapDrawable());

return window;
}

/**
* Displays like a QuickAction from the anchor view.
*
* @param xOffset
* offset in the X direction
* @param yOffset
* offset in the Y direction
*/
public static void showLikeQuickAction(PopupWindow window, View root,
View anchor, WindowManager windowManager, int xOffset, int yOffset) {

window.setAnimationStyle(R.style.Animations_GrowFromTop);

int[] location = new int[2];
anchor.getLocationOnScreen(location);

Rect anchorRect = new Rect(location[0], location[1], location[0]
+ anchor.getWidth(), location[1] + anchor.getHeight());

root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

int rootWidth = root.getMeasuredWidth();
int rootHeight = root.getMeasuredHeight();

int screenWidth = windowManager.getDefaultDisplay().getWidth();
int screenHeight = windowManager.getDefaultDisplay().getHeight();

int xPos = ((screenWidth - rootWidth) / 2) + xOffset;
int yPos = anchorRect.top - rootHeight + yOffset;

window.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);
}

}

最后将此代码添加到您的activity.java 文件中

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();
if (id == R.id.menus) {
final View menuItemView = findViewById(R.id.menus);
showPopup(menuItemView);
return true;
}
return super.onOptionsItemSelected(item);
}

private void showPopup(View view) {
PopupWindow showPopup = PopupHelper
.newBasicPopupWindow(getApplicationContext());
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.custom_layout, null);
showPopup.setContentView(popupView);

showPopup.setWidth(LayoutParams.WRAP_CONTENT);
showPopup.setHeight(LayoutParams.WRAP_CONTENT);
showPopup.setAnimationStyle(R.style.Animations_GrowFromTop);
showPopup.showAsDropDown(view);
}

关于android - 如何在 GridView 中显示菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27418917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com