0

具有所需布局的图像:

在此处输入图像描述我有以下情况:

我有一个包含从数据库填充的按钮的组(例如,特定表中的 10 个项目将填充该组中的 10 个按钮) - 工作正常。

我不知道该怎么做的部分是:

  • 我希望这个按钮将显示为一个圆圈(可绘制),并且在该圆圈内有一个图像(路径将从数据库中获取)和上述说明中的标签(来自表格)。

  • 如果有一个项目,它将位于屏幕中央,并且随着按钮数量的增加,圆圈将设置在 3 列的网格内。

有人可以帮我解决这个问题吗?

我尝试使用不同方法(使用 RadioButtons)的旧版本,在此示例中没有 Image 或 drawable,并且项目是 RadioGroup 中的 radioButtons:

DatBas dbc = new DatBas(Tamar_appActivity.this);
    dbc.open();
    SQLiteDatabase tdb = dbc.getDatabase();
    String[] columns = new String[] { DatBas.KEY_ROW_B_ID,
            DatBas.KEY_B_NAME };
    Cursor c = tdb.query(DatBas.DATABASE_TABLE_SETTINGS, columns, null,
            null, null, null, null);

    int iRawBId = c.getColumnIndex(DatBas.KEY_ROW_ID);
    int iBName = c.getColumnIndex(DatBas.KEY_B_NAME);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

        RadioGroup radiogroup = (RadioGroup)   
                    findViewById(R.id.bNameSelectGroup);
        final Button rdbtn = new Button(this);

        rdbtn.setId(iRawBId);
        rdbtn.setText(c.getString(iBName));
        radiogroup.addView(rdbtn);
    }
    dbc.close();

}

这是 SpacialButton.java 代码:

public class SpecialButton extends RelativeLayout {

public TextView text;
//public ImageView image;

public SpecialButton(Context context) {
    super(context);
    this.setBackgroundResource(R.drawable.cb_shape);
    LayoutInflater.from(context).inflate(R.layout.cb_inner,
            this, true);
    text = (TextView) findViewById(R.id.textView1);
//    image = (ImageView) findViewById(R.id.imageBB);
}

public SpecialButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public SpecialButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}

}

这是 cb_inner.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- This will hold the image from the database -->
    <ImageView
        android:id="@+id/imageBB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <View
        android:id="@+id/anchor"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true" />

    <!-- This will be the label. It will be placed below the center -->
    <TextView
        android:id="@+id/cbTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/anchor"
        android:layout_centerHorizontal="true"
        android:text="TextView"
        android:textColor="#000000" />

</merge>

这是 Main.java 代码:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TableLayout tl = (TableLayout) findViewById(R.id.grid);
            Display display2 = getWindowManager().getDefaultDisplay(); 
            int width = display2.getWidth();  
            int height = display2.getHeight(); 
            int realSize = width / 3;


        DatBas dbc = new DatBas(Tamar_appActivity.this);
        dbc.open();
        SQLiteDatabase tdb = dbc.getDatabase();
        String[] columns = new String[] { DatBas.KEY_ROW_B_ID,
                DatBas.KEY_B_NAME };
        Cursor c = tdb.query(DatBas.DATABASE_TABLE_SETTINGS, columns, null,
                null, null, null, null);
        c.moveToFirst();
        int iRawBId = c.getColumnIndex(DatBas.KEY_ROW_B_ID);
        int iBName = c.getColumnIndex(DatBas.KEY_B_NAME);
        int iBimage = c.getColumnIndex(DatBas.KEY_B_IMAGE_PATH);
//      ic_launcher.png
        int cursorSize = c.getCount();
        int rows = (cursorSize / 3) + 1; // +1 so you have at least one row if the number of button is below 3
        int current = 1; // starting position
        for (int i = 0; i < rows; i++) {
            TableRow tr1 = new TableRow(this);
            tr1.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));
            for (int j = 0; j < 3; j++) {
                if (current <= cursorSize) {
                    SpecialButton sb = new SpecialButton(this);
                    sb.text.setText(c.getString(iBName));
                    sb.setLayoutParams(new TableRow.LayoutParams(realSize,
                            realSize));
                    // other stuff you would do
//                  sb.image.getDrawable();
                    tr1.addView(sb);
                    c.moveToNext();
                }
                current++;
            }
            tl.addView(tr1);
        }
        dbc.close();

    }
4

1 回答 1

0

我希望这个按钮将显示为一个圆圈(可绘制),在该圆圈内有一个图像(路径将从数据库中获取),以及来自上述说明的标签(来自表格)。

我建议您制作一个布局文件来模拟所有项目:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- This will hold the image from the database -->
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <View
        android:id="@+id/anchor"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true" />

    <!-- This will be the label. It will be placed below the center -->
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/anchor"
        android:layout_centerHorizontal="true"
        android:text="TextView"
        android:textColor="#000000" />

</merge>

上面使用的item_backgrounddrawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <solid android:color="#92c47d" />

    <stroke
        android:width="2dp"
        android:color="#000000" />

</shape>

然后你可能想把它放在一个自定义View类中以便于处理:

    public class SpecialButton extends RelativeLayout {

    public TextView text;
    public ImageView image;

    public SpecialButton(Context context) {
        super(context);
        this.setBackgroundResource(R.drawable.item_background1);
        LayoutInflater.from(context).inflate(R.layout.views_special_btn_layout,
                this, true);
        text = (TextView) findViewById(R.id.textView1);
        image = (ImageView) findViewById(R.id.image);
    }

    public SpecialButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SpecialButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

}

模拟一个Button添加OnCLickListener到上面的自定义视图。没有办法在 xml drawable 中创建一个圆圈(据我所知),所以如果你没有View上面自定义的确切大小(比如 的值100dp),你将在代码中计算它。此外,如果您想将TextView 标签放置在某个特定位置,则必须再次在代码中执行此操作。我不确切知道您希望如何将按钮放置在屏幕上,但通常您会找出屏幕的宽度和高度并从中计算实际尺寸:

// in the onCreate method
Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  
int height = display.getHeight(); 
// here I assumed you have only 3 buttons and you want them to be placed taking all
// the width of the screen(of course this isn't ideal because if the user turns the
// phone, in landscape, you'll end up with 3 really big buttons so you may want to   
// set a default size for the buttons )
int realSize = width / 3; 
Cursor c = tdb.query(DatBas.DATABASE_TABLE_SETTINGS, columns, null,
            null, null, null, null);
    int iRawBId = c.getColumnIndex(DatBas.KEY_ROW_ID);
    int iBName = c.getColumnIndex(DatBas.KEY_B_NAME);
    int cursorSize = c.getCount();
    int rows = (cursorSize / 3) + 1; // +1 so you have at least one row if the number of button is below 3
    int current = 1; // starting position
    for (int i = 0; i < rows; i++) {
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableLayout.LayoutParams(
                TableLayout.LayoutParams.FILL_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT));
        for (int j = 0; j < 3; j++) {
            if (current <= cursorSize) {
                SpecialButton sb = new SpecialButton(this);
                sb.text.setText(c.getString(iBName));
                sb.setLayoutParams(new TableRow.LayoutParams(realSize,
                        realSize));
                // other stuff you would do
                tr.addView(sb);
                c.moveToNext();
            }
            current++;
        }
        tl.addView(tr);
    }

如果有一个项目,它将位于屏幕的中心,并且随着按钮数量的增长,圆圈将设置在 3 列的网格内。

我不知道您当前的布局,但这可以在这样的布局中非常容易地完成(我假设您想将项目放在 a 中TableLayout):

<?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="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableLayout>

</LinearLayout>

然后,您将元素放置在代码中(每个 3 项在 a 中TableRow),并且gravity parentLinearLayout属性的 应该将它们放在中心。您可以使用 aGridView而不是 a,TableLayout但您必须努力使项目居中。上面 3 个按钮的示例:

       // in the onCreate method
       tl = (TableLayout) findViewById(R.id.grid);
        Display display = getWindowManager().getDefaultDisplay(); 
        int width = display.getWidth();  
        int height = display.getHeight(); 
        int realSize = width / 3;
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableLayout.LayoutParams(
                TableLayout.LayoutParams.FILL_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT));
        SpecialButton sb1 = new SpecialButton(this);
        sb1.setLayoutParams(new TableRow.LayoutParams(realSize, realSize));
        SpecialButton sb2 = new SpecialButton(this);
        sb2.setLayoutParams(new TableRow.LayoutParams(realSize, realSize));
        SpecialButton sb3 = new SpecialButton(this);
        sb3.setLayoutParams(new TableRow.LayoutParams(realSize, realSize));
        tr.addView(sb1);
        tr.addView(sb2);
        tr.addView(sb3);
        tl.addView(tr);
于 2012-05-31T18:33:41.703 回答