在我的应用程序的一项活动中,我有一个按钮,每次单击该按钮时我要在其中显示图像。例如:
通过单击我的活动按钮,图像显示在屏幕上,如图所示。
第二次及之后单击按钮将导致新图像相应地追加。
我想对如何实现这个建议。
我使用TextView进行了与此类似的操作。基本上我是这样做的:
XML:
就我而言,我提出了TableLayout
例如:
<TableLayout
android:id="@+id/existedTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_standard">
<TableRow>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/number_text"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />
</TableRow>
</TableLayout>
活动
注意:根据情况将其更改为ImageView
/* //Get the TableLayout. Ex:
private TableLayout existedTableLayout = findViewById(R.id.existedTableLayout);
*/
/* Make onClickListerner to call below function */
private void addTableRowDynamically() {
//Make new Row
TableRow newRow= new TableRow(this);
TextView newNoTextView = new TextView(this);
//some TextView method, do your research about ImageView
newNoTextView.setLayoutParams(new TableRow.LayoutParams(0,ViewGroup.LayoutParams.WRAP_CONTENT, 1));
newNoTextView.setText("this is text");
newNoTextView.setTextAppearance(this, android.R.style.TextAppearance_DeviceDefault_Large);
// Add the TextView to the newRow
newRow.addView(newNoTextView);
// Add the newRow which contain the TextView to the TableLayout, below
existedTableLayout.addView(newRow, existedTableLayout.getChildCount());
}
添加垂直LineaLayout
并动态添加视图:
private void createViews() {
for (int i = 0; i < numberOfViews; i++) {
view = new ImageView(context);
int width = 300;
int height = 50;
view.setPadding(18, 10, 0, 0);
view.setLayoutParams(new LinearLayout.LayoutParams(width, height));
view.setId(i);
view.setGravity(Gravity.CENTER_VERTICAL);
view.setBackgroundColor(Color.parseColor("someColor"));
viewList.add(view);
}
}
Rect rectf = new Rect();
for (ImageView view : viewList) {
view.getGlobalVisibleRect(rectf);
coordinates.add(rectf.bottom);
}