所以我正在尝试创建一个棋盘游戏令牌,并将该令牌放在它的起点。恰好在这个级别上,它从ImageView onetwo开始。但是,当我创建令牌并告诉它去哪里时,它会显示在gridview下。任何想法或帮助将不胜感激!
我猜它是因为我在网格视图中使用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>
答案 0 :(得分:1)
如果您想将布局元素叠加在一起,那么您应该使用FrameLayout
。
答案 1 :(得分:1)
我最终以编程方式创建了一个imageview,然后将其放在imageview顶部的相对布局中:
<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" >
<RelativeLayout
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_columnSpan="1"
android:layout_rowSpan="1"
android:id="@+id/LLoneone">
<ImageView
android:id="@+id/oneone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/gameboard"
android:gravity="center"
android:src="@drawable/tile" />
</RelativeLayout>