所以我正在尝试创建一个棋盘游戏 token ,并将该 token 放置在它的起点。正是在这个级别上发生了这种情况,它从 ImageView onetwo 之上开始。但是,当我创建 token 并告诉它去哪里时,它会显示在 GridView 下。任何想法或帮助将不胜感激!
我猜这是因为我在 GridView 中使用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>
最佳答案
如果您想将布局元素堆叠在一起,那么您应该使用 FrameLayout
。
关于java - Android 以编程方式在另一个 ImageView 之上添加一个 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19409311/