【问题标题】:Android action button issueAndroid操作按钮问题
【发布时间】:2013-11-01 16:43:24
【问题描述】:

我在操作栏中有一个菜单项。如下图所示,它总是看起来很小。

我希望它的大小约为。这个的两倍。我在 mdpi、hdpi、xhdpi 文件夹中放置了不同大小的图标。因此,这不是 dpi 相关问题。

我在 HTC One 和 xhdpi 模拟器中遇到了这个问题。我尝试在 xhdpi 文件夹中放大图标并在模拟器中进行测试,但在操作栏中其大小没有变化。对于 micromax canvas2(hdpi) 它工作正常。

有没有办法设置菜单项的大小来固定高度和宽度?或者任何其他解决方法?

【问题讨论】:

  • 可以放你的xml代码吗?
  • @Sushil xml 用于菜单项?还是整个布局?为了更清楚,这是所有在操作栏中具有菜单项的屏幕的问题。所以可能只有菜单项代码有问题。
  • 也许你可以使用自定义标题栏并且可以控制菜单项的大小。在那种情况下,根本不要使用菜单项。我在下面发布了一个自定义标题栏代码。
  • 你可以这样做:设置菜单项值或drawable,然后获取密度dpi值并检查其xhdpi是否是,设置宽度n高度
  • @DharaShah 正如问题所述,我没有找到任何方法来设置菜单项的大小。

标签: android android-actionbar


【解决方案1】:

我有同样的问题,到目前为止我还没有找到解决方案。这是 HTC 特有的,我在 HTC One X 上观察到了这种行为。

他们的 Sense 框架强制使用方形菜单图标,因此它会缩小您的视野。作为一种解决方法,我没有使用菜单项 api,我为操作栏创建了一个自定义视图,并使用以下答案将我的图像视图对齐到右侧(同时保留标题和主页图标):SherlockActionBar: How to adjust CustomView against actionBar

【讨论】:

  • 我也可以确认这是 HTC 感知问题。
【解决方案2】:

也许你可以使用自定义标题栏:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@drawable/action_bar"
    android:orientation="horizontal" >

<LinearLayout
    android:id="@+id/slidingmenu"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:paddingLeft="20dip"
    android:layout_centerInParent="true" 
    android:layout_alignParentLeft="true">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/slidingmenu_selector" 
        android:duplicateParentState="true" />
 </LinearLayout>


<TextView
        android:id="@+id/myheading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#000000"
        android:textStyle="bold"
        android:textSize="23dp" />

<LinearLayout
    android:id="@+id/homemenu"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:paddingRight="20dip"
    android:layout_centerInParent="true" 
    android:layout_alignParentRight="true">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/homemenu_selector" 
        android:duplicateParentState="true" />
 </LinearLayout>


</RelativeLayout>

【讨论】:

  • 你能解释一下第一个 ImageView 是干什么用的,第二个 ImageView 是干什么用的?另外,我的操作栏没有固定数量的菜单项。
  • 我在左侧有一个用于打开滑块的菜单项,在右侧有一个用于主页按钮的菜单。
  • 好的。 ImageView 会响应点击吗?
  • 是的。这是正常的图像视图,您可以为其设置一个 clicklistener。
  • 则对用户点击事件无效。但更令人担忧的是,为什么我无法使用操作栏?我有什么问题?如果是action bar,那么其他人也应该有这个问题吧?而且我可能不想在我的所有应用程序中使用这个解决方法,因为 SDK 已经实现了这个,我将重做它。
【解决方案3】:

将图片放入drawable-nodpi文件夹

【讨论】:

    【解决方案4】:

    我通过使用 menuitem 的自定义视图解决了这个问题。

    菜单项布局:

        <ImageButton
            android:id="@+id/first_menu_item"
            android:layout_width="56dp"
            android:layout_height="33dp"
            android:layout_gravity="center"
            android:contentDescription=""
            android:background="@drawable/menu_item_background"
            android:gravity="center" />
    
    </LinearLayout>
    

    为活动中的操作栏设置自定义视图:

    // Set custom menu items for action bar
    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                                | Gravity.CENTER_VERTICAL);   // Set Layout Parameters
    lp.setMargins(5, 5, 10, 5); // Left, top, right, bottom margins
    View view = LayoutInflater.from(this).inflate(
                        R.layout.actionbar_menu_item, null);
    ImageButton menuItem = (ImageButton) view
                        .findViewById(R.id.first_menu_item);
    menuItem.setImageDrawable(getResources().getDrawable(
                        R.drawable.edit_button));
    menuItem.setOnClickListener(this);
    actionBar.setCustomView(view, lp);
    actionBar.setDisplayShowCustomEnabled(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多