目前我有一个用imageviews设计的UI,问题是它们不能重叠或分层。 (或者,如果他们可以,我还没弄清楚如何)
我想做3层,底层是板,
下一层是碎片,
下一层是具有有效移动的选定块。
我的想法是,我只会在顶层有听众。
我只是在寻找最适合的数据结构,这个应用程序是我正在开发的象棋游戏。
如果有另一种方法可能会更好,我也是耳朵。
谢谢!
按要求的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game Activity"
android:id="@+id/textView5"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView22"
android:src="@drawable/ni_whitesquare"
android:layout_below="@+id/textView5"
android:layout_alignRight="@+id/textView5"
android:layout_alignEnd="@+id/textView5"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="76dp"
android:contentDescription="whitesquare"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView32"
android:src="@drawable/ni_blacksquare"
android:layout_alignTop="@+id/imageView22"
android:layout_alignLeft="@+id/imageView22"
android:layout_alignStart="@+id/imageView22"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"
android:contentDescription="blacksquare"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView12"
android:src="@drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_alignTop="@+id/imageView22"
android:layout_toLeftOf="@+id/imageView22"
android:layout_toStartOf="@+id/imageView22" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView21"
android:src="@drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_below="@+id/imageView22"
android:layout_toRightOf="@+id/imageView12"
android:layout_toEndOf="@+id/imageView12" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView23"
android:src="@drawable/ni_blacksquare"
android:contentDescription="blacksquare"
android:clickable="true"
android:layout_above="@+id/imageView12"
android:layout_toLeftOf="@+id/imageView33"
android:layout_toStartOf="@+id/imageView33" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView31"
android:src="@drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignBottom="@+id/imageView21"
android:layout_alignLeft="@+id/imageView32"
android:layout_alignStart="@+id/imageView32" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView11"
android:src="@drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="@+id/imageView21"
android:layout_toLeftOf="@+id/imageView21"
android:layout_toStartOf="@+id/imageView21" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView13"
android:src="@drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="@+id/imageView23"
android:layout_alignLeft="@+id/imageView12"
android:layout_alignStart="@+id/imageView12" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView33"
android:src="@drawable/ni_whitesquare"
android:contentDescription="whitesquare"
android:clickable="true"
android:layout_alignTop="@+id/imageView23"
android:layout_alignLeft="@+id/imageView32"
android:layout_alignStart="@+id/imageView32" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/directions"
android:id="@+id/directionsstring"
android:layout_marginTop="30dp"
android:layout_below="@+id/imageView21"
android:layout_alignLeft="@+id/textView5"
android:layout_alignStart="@+id/textView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Startingpoint"
android:src="@drawable/ni_pawn"
android:layout_above="@+id/imageView11"
android:layout_toLeftOf="@+id/imageView12"
android:layout_toStartOf="@+id/imageView12" />
</RelativeLayout>
活动:
public class GameActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final ImageView mStarting = (ImageView) findViewById(R.id.Startingpoint);
final ImageView mCenterSq = (ImageView) findViewById(R.id.imageView22);
final ImageView m12 = (ImageView)findViewById(R.id.imageView12);
mCenterSq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
m12.setImageResource(R.drawable.ni_blacksquare);
mCenterSq.setImageResource(R.drawable.ni_portal);
//center square turns into a portal when clicked.
if (mStarting.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_whitesquare).getConstantState()) &&
mCenterSq.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_portal).getConstantState()) )
{
mCenterSq.setOnClickListener(null);
return;
}
mStarting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
//Pawn selected
mStarting.setImageResource(R.drawable.ni_whitesquare);
m12.setImageResource(R.drawable.ni_greensquare);
mCenterSq.setImageResource(R.drawable.ni_greensquare);
if (mStarting.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_whitesquare).getConstantState()))
{
mStarting.setOnClickListener(null);
}
m12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if (m12.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.ni_greensquare).getConstantState()))
{
m12.setImageResource(R.drawable.ni_pawn);
mCenterSq.setImageResource(R.drawable.ni_portal);
}
}
});
}
});
};
});
}
}
答案 0 :(得分:1)
对于分层视图,您可能希望使用FrameLayout,请参阅以下答案:Layout Layers? Z-Axis?
此外,如果您尝试设置一个布局,其中您有几个连续的视图需要一个接一个地排列,最合适的元素通常是LinearLayout。根据您为其设置的android:orientation属性,LinearLayout将按照您声明的顺序按连续顺序自动设置视图。
总的来说,我建议查看每个文档。 RelativeLayout可能不是一种可行的方式,因为它涉及大量的视图定位管理,这对我认为你想做的事情来说似乎是无聊的。我希望这有帮助