0

我想做仪表板模式。我目前为每个主页按钮执行此操作:

<FrameLayout
    android:layout_weight="1"
    android:focusable="true"
    android:onClick="onHomeButtonClicked"
    android:background="@drawable/button_background">
    <TextView
        android:text="@string/button_text"
        android:drawableTop="@drawable/button_icon"
        android:layout_gravity="center"
        android:gravity="center"
        android:background="@android:color/transparent" />
</FrameLayout>

我将按钮包装在 FrameLayout 中的原因是我想要:

  • 最大化可点击区域
  • 使图标和文本正确居中。

我过去尝试过这样做,但放弃了,因为我无法找到一种与屏幕尺寸无关的方式来使文本和图标居中:

<Button
    android:layout_weight="1"
    android:background="@drawable/button_background"
    android:text="@string/button_text"
    android:onClick="onHomeButtonClicked"
    android:drawableTop="@drawable/button_icon"
    android:drawablePadding="DONT_KNOW_WHAT_TO_PUT_IN_HERE" />

我的问题:是否有可能做到所有这些:

  1. 不将 Button 包装在其他布局中(每个按钮仅使用 1 个视图)
  2. 最大化可点击区域
  3. 以独立于屏幕大小的方式正确居中图标和文本

非常感谢。

4

1 回答 1

0

这是我用来以编程方式构建仪表板的一些代码:

@Override public View getView(int position, View convertView,
        ViewGroup parent) {
    TextView v = (TextView) convertView;
    if (v == null) {
        v = new TextView(DashboardActivity.this);
        v.setGravity(Gravity.CENTER);
    }
    v.setCompoundDrawablesWithIntrinsicBounds(0,
        mIcons[position].drawableId, 0, 0);
    v.setText(mIcons[position].labelId);

    return v;
}

如果您可以预先构建仪表板,则可以在 XML 文件中使用 android:drawableTop。

于 2011-05-08T07:27:13.510 回答