具有所需布局的图像:
我有以下情况:
我有一个包含从数据库填充的按钮的组(例如,特定表中的 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();
}