【问题标题】:Gridview button configurationGridview 按钮配置
【发布时间】:2011-04-18 22:02:55
【问题描述】:

我有一个 Gridview 显示按钮,这些按钮从数组中获取内容,请参见下文:

xml:

<LinearLayout
    android:background="@raw/br_poster_dark"
    android:orientation="vertical"    
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_gravity="center"
    android:padding="30dip"     >   



            <GridView android:id="@+id/brgrid"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:padding="5dp"
                android:columnWidth="90dp"
                android:rowHeight="90dp"
                android:numColumns="3"
                android:gravity="center"
                android:textColor="#ffffff"
                />

</LinearLayout>

还有按钮的java:

public View getView(int position, View convertView, ViewGroup parent) {
    try {
        final Sample sample = board.getSamples().get(position);

        if (sample != null) {
            Button button = new Button(context);
            button.setText(sample.getName());


            button.setBackgroundResource(R.drawable.roundedbutton);

如您所见,我有一个自定义背景(透明渐变和圆角),效果很好,但我很难更改按钮的其他属性,因为大多数代码通常会进入 xml 下“按钮”:

比如改变文字颜色不起作用,改变GridView xml下的行/列大小没有效果……

我还想让它显示一个具有统一按钮大小的等距网格,而不是将传递给按钮的文本包装起来并将它们塞满屏幕顶部。

有没有一种方法可以创建 Gridview 使用的 xml 按钮?

提前致谢。

【问题讨论】:

    标签: android gridview button


    【解决方案1】:

    我找到了一种使用密度比例设置按钮的文本大小和布局的方法:

    button.setLayoutParams(new GridView.LayoutParams((int)(80 * densityScale + 0.5f), (int)(50 * densityScale + 0.5f)));
    
    button.setTextSize(12);
    

    【讨论】:

      【解决方案2】:

      我不明白在代码中声明按钮的必要性。 将其放入 de xml 并通过使用 finViewById() 检索按钮来设置文本

      希望对你有所帮助

      【讨论】:

      • 我明白你在说什么,这也意味着改变我的阵列播放相应声音文件的方式。我认为可能有一种方法可以在 java 中为我省去麻烦。我需要的只是改变重量和文字。感谢您的帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 2012-09-17
      • 2015-03-26
      相关资源
      最近更新 更多