【发布时间】:2014-01-03 17:01:21
【问题描述】:
这是XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mylayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/whitebar" />
</RelativeLayout>
这是 Java 文件
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.mylayout);
int images[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4};
relativeLayout.setBackgroundResource(images[getRandomNumber()]);
}
private int getRandomNumber() {
//Note that general syntax is Random().nextInt(n)
//It results in range 0-4
//So it should be equal to number of images in images[] array
return new Random().nextInt(4);
}}
The code above gives an output
BUT我想发生的事情类似于
由于相对布局重力不起作用,如何设置我的白条图像的确切位置?
【问题讨论】:
标签: android android-layout android-relativelayout