gpt4 book ai didi

Android 将菜单项在操作栏中向左对齐

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:27:55 24 4
gpt4 key购买 nike

我的应用程序中有操作栏,它显示在我的 res/menu/activity_main.xml 中定义的菜单项

我的菜单项在操作栏上右对齐。我希望它们向左对齐。

我为这个使用过的自定义操作栏找到的唯一解决方案,比如这个: Positioning menu items to the left of the ActionBar in Honeycomb

但是,我不想为我的菜单创建自定义布局。我想使用从我的 res/menu/activity_main.xml 生成的默认菜单项。

这可能吗?

最佳答案

嗯,我对此很好奇,所以我深入挖掘了 SDK 的源代码。我在它的 XML 文件中使用了带有 3 个菜单项的 AppCompatActivity,并且我使用了默认的 onCreateOptionMenu 方法,它是这样的:

@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);
return true;
}

在我使用调试器从 inflate 方法继续前进之后,我经历了以下堆栈:

updateMenuView():96, BaseMenuPresenter (android.support.v7.internal.view.menu)
updateMenuView():231, ActionMenuPresenter (android.support.v7.widget)
dispatchPresenterUpdate():284, MenuBuilder (android.support.v7.internal.view.menu)
onItemsChanged():1030, MenuBuilder (android.support.v7.internal.view.menu)
startDispatchingItemsChanged():1053, MenuBuilder (android.support.v7.internal.view.menu)
preparePanel():1303, AppCompatDelegateImplV7 (android.support.v7.app)
doInvalidatePanelMenu():1541, AppCompatDelegateImplV7 (android.support.v7.app)
access$100():92, AppCompatDelegateImplV7 (android.support.v7.app)
run():130, AppCompatDelegateImplV7$1 (android.support.v7.app)
handleCallback():739, Handler (android.os)
dispatchMessage():95, Handler (android.os)
loop():148, Looper (android.os)
main():5417, ActivityThread (android.app)
invoke():-1, Method (java.lang.reflect)
run():726, ZygoteInit$MethodAndArgsCaller (com.android.internal.os)
main():616, ZygoteInit (com.android.internal.os)

它结束于 BaseMenuPresenterupdateMenuView 方法,这是完成相关工作的地方。

方法代码:

public void updateMenuView(boolean cleared) {
final ViewGroup parent = (ViewGroup) mMenuView;
if (parent == null) return;

int childIndex = 0;
if (mMenu != null) {
mMenu.flagActionItems();
ArrayList<MenuItemImpl> visibleItems = mMenu.getVisibleItems();
final int itemCount = visibleItems.size();
for (int i = 0; i < itemCount; i++) {
MenuItemImpl item = visibleItems.get(i);
if (shouldIncludeItem(childIndex, item)) {
final View convertView = parent.getChildAt(childIndex);
final MenuItemImpl oldItem = convertView instanceof MenuView.ItemView ?
((MenuView.ItemView) convertView).getItemData() : null;
final View itemView = getItemView(item, convertView, parent);
if (item != oldItem) {
// Don't let old states linger with new data.
itemView.setPressed(false);
ViewCompat.jumpDrawablesToCurrentState(itemView);
}
if (itemView != convertView) {
addItemView(itemView, childIndex);
}
childIndex++;
}
}
}

// Remove leftover views.
while (childIndex < parent.getChildCount()) {
if (!filterLeftoverView(parent, childIndex)) {
childIndex++;
}
}
}

这里的 getItemViewaddItemView 方法的作用与它们的名称相同。第一个膨胀一个新 View ,第二个将其添加到父 View 。更重要的是,在调试器下可以检查父对象,它是一个ActionMenuView。 , 它继承自 LinearLayout 和膨胀形式 abc_action_menu_layout.xml .

这意味着如果你能得到这个 View ,你就可以做你想做的事。从理论上讲,我认为可以通过大量反射(reflection)来完成,但这会很痛苦。相反,您可以在您的代码中重现它。可以找到实现 here .

根据以上情况,你的问题的答案是肯定的,可以做到,但会很棘手。

编辑:

我通过反射(reflection)创建了一个概念证明。我用过 com.android.support:appcompat-v7:23.1.0

我已经在模拟器(Android 6.0)和我的 Zuk Z1(CM Android 5.1.1)上试过了,两者都运行良好。

菜单 XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="always" />
<item android:id="@+id/action_settings2" android:title="TEST1"
android:orderInCategory="100" app:showAsAction="always" />
<item android:id="@+id/action_settings3" android:title="TEST2"
android:orderInCategory="100" app:showAsAction="always" />
</menu>

Activity XML:

<LinearLayout 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">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center_vertical" />
</LinearLayout>

Activity :

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//only a linear layout with one button
setContentView(R.layout.activity_main2);

Button b = (Button) findViewById(R.id.button);

// do the whole process for a click, everything is inited so we dont run into NPE
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

AppCompatDelegate delegate = getDelegate();

Class delegateImpClass = null;
Field menu = null;
Method[] methods = null;

try {

//get objects based on the stack trace
delegateImpClass = Class.forName("android.support.v7.app.AppCompatDelegateImplV7");

//get delegate->mPreparedPanel
Field mPreparedPanelField = delegateImpClass.getDeclaredField("mPreparedPanel");
mPreparedPanelField.setAccessible(true);
Object mPreparedPanelObject = mPreparedPanelField.get(delegate);

//get delegate->mPreparedPanel->menu
Class PanelFeatureStateClass = Class.forName("android.support.v7.app.AppCompatDelegateImplV7$PanelFeatureState");
Field menuField = PanelFeatureStateClass.getDeclaredField("menu");
menuField.setAccessible(true);
Object menuObjectRaw = menuField.get(mPreparedPanelObject);
MenuBuilder menuObject = (MenuBuilder) menuObjectRaw;

//get delegate->mPreparedPanel->menu->mPresenter(0)
Field mPresentersField = menuObject.getClass().getDeclaredField("mPresenters");
mPresentersField.setAccessible(true);
CopyOnWriteArrayList<WeakReference<MenuPresenter>> mPresenters = (CopyOnWriteArrayList<WeakReference<MenuPresenter>>) mPresentersField.get(menuObject);
ActionMenuPresenter presenter0 = (ActionMenuPresenter) mPresenters.get(0).get();

//get the view from the presenter
Field mMenuViewField = presenter0.getClass().getSuperclass().getDeclaredField("mMenuView");
mMenuViewField.setAccessible(true);
MenuView menuView = (MenuView) mMenuViewField.get(presenter0);
ViewGroup menuViewParentObject = (ViewGroup) ((View) menuView);

//check the menu items count
int a = menuViewParentObject.getChildCount();
Log.i("ChildNum", a + "");




//set params as you want
Toolbar.LayoutParams params = (Toolbar.LayoutParams) menuViewParentObject.getLayoutParams();

params.gravity = Gravity.LEFT;

menuViewParentObject.setLayoutParams(params);




} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
});
}
@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);
return true;
}
}

虽然这里的重力已经改变,但在屏幕上这并没有产生任何相关的差异。要获得真正可见的变化,应调整其他布局参数(例如 width )。

总而言之,自定义布局更易于使用。

关于Android 将菜单项在操作栏中向左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12939085/

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