所以我正在尝试创建一个棋盘游戏令牌,并将该令牌放在它的起点。就这样发生在这个级别上,它从 ImageView onetwo 的顶部开始。但是,当我创建令牌并告诉它去哪里时,它会显示在网格视图下。任何想法或帮助将不胜感激!
我猜是因为我在网格视图中使用 RelativeLayout.ALIGN_TOP 但我没有看到替代方案。
爪哇:
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>