所以我试图创建一个棋盘游戏令牌,并将该令牌放置在它的起点上。就在这个层面上发生的事情,它在ImageView上开始。但是,当我创建令牌并告诉它去哪里时,它显示在gridview下。任何想法或帮助将不胜感激!Android以编程方式在另一个ImageView上添加一个ImageView
我想它是因为我在网格视图内使用RelativeLayout.ALIGN_TOP,但我没有看到替代方案。
的Java:
public void createToken(){
// Let's create the missing ImageView
ImageView image = new ImageView(this);
// Now the layout parameters, these are a little tricky at first
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(75, 75);
params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.onetwo);
params.addRule(RelativeLayout.ALIGN_LEFT, R.id.onetwo);
params.addRule(RelativeLayout.ALIGN_TOP, R.id.onetwo);
params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.onetwo);
image.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
image.setImageResource(R.drawable.robo);
// Let's get the root layout and add our ImageView
GridLayout layout = (GridLayout) findViewById(R.id.gridLayout1);
layout.addView(image, params);
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/root" >
...
<GridLayout
android:id="@+id/gridLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:columnCount="3"
android:rowCount="5" >
...
<ImageView
android:id="@+id/onetwo"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_columnSpan="1"
android:layout_rowSpan="1"
android:contentDescription="@string/gameboard"
android:gravity="center"
android:src="@drawable/tile" />
...
</GridLayout>
...
</RelativeLayout>
这是为什么.... – jcaruso
阅读的FrameLayout第一文档:HTTP:/ /developer.android.com/reference/android/widget/FrameLayout.html 最重要的是这一行:'子视图被绘制在一个堆栈中,最近添加的子项在顶部.'只需小心测试不同的屏幕大小它说如果你走这条路。 –