我有一个线性布局,其中包含10个图像(例如啤酒杯)5个在上排,5个在第二排但我想动态显示这些图像,就像我们从服务器获得3个值然后两个啤酒杯将是在中间也显示在上排,第3个杯子也在中心也显示在第2行。 同样,如果我们从服务器获得7个值,那么4个杯子将显示在上排,3个显示在第二排。
答案 0 :(得分:0)
我认为你正在寻找这样的东西。
import java.util.Random;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity {
private Context context = null;
private Button btnUpdate = null;
private LinearLayout firstRow = null;
private LinearLayout secondRow = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
this.firstRow = (LinearLayout) this
.findViewById(R.id.ac_main__firstrow);
this.secondRow = (LinearLayout) this
.findViewById(R.id.ac_main__secondrow);
this.btnUpdate = (Button) this.findViewById(R.id.ac_main__updatebtn);
this.btnUpdate.setOnClickListener(onClickUpdateRow);
}
// I used this mothods only because i don't have a server that give me the value
private int generateRandomInt() {
Random rdm = new Random();
return rdm.nextInt(9);
}
// Generate a new ImageView
private ImageView generateImageView() {
ImageView beerIcon = new ImageView(this.context);
//Custumize this with your drawable
beerIcon.setImageDrawable(this.getResources().getDrawable(R.drawable.ic_launcher));
return beerIcon;
}
private void updateImageRow() {
int rdmValue = this.generateRandomInt();
int rowOneImageNumber = rdmValue / 2;
int rowTwoImageNumber = rdmValue - rowOneImageNumber;
this.firstRow.removeAllViewsInLayout();
this.secondRow.removeAllViewsInLayout();
if (rowOneImageNumber >= rowTwoImageNumber) {
for (int i = 0; i <= rowOneImageNumber; i++) {
this.firstRow.addView(this.generateImageView());
}
for (int i = 0; i <= rowTwoImageNumber; i++) {
this.secondRow.addView(this.generateImageView());
}
} else {
for (int i = 0; i <= rowTwoImageNumber; i++) {
this.firstRow.addView(this.generateImageView());
}
for (int i = 0; i <= rowOneImageNumber; i++) {
this.secondRow.addView(this.generateImageView());
}
}
}
private OnClickListener onClickUpdateRow = new OnClickListener() {
@Override
public void onClick(View arg0) {
updateImageRow();
}
};
而actvity_main.xml
就像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ac_main__firstrow"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/ac_main__secondrow"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:orientation="horizontal">
</LinearLayout>
<Button
android:id="@+id/ac_main__updatebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update" />
<!-- other layout element -->
</LinearLayout>
希望这会让你朝着正确的方向前进。