HeyGuys,
第二天在Android Studio上,请原谅。 基本上,我有一副可供用户点击的纸牌。点击后,我执行layout.addView();。这可行。
当我希望下一张卡与当前卡重叠时出现问题。我尝试设置负边距,仅裁剪每张图像。我缺少什么(可能很明显)?
{编辑:我的b)包含布局代码
活动:
public class NewGame extends AppCompatActivity {
RelativeLayout mRelativeLayout;
LinearLayout p1_cards;
ImageView center_deck;
private Random r = new Random();
private ArrayList<Cards> mCards;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newgame);
center_deck = (ImageView) findViewById(R.id.center_deck);
mRelativeLayout = (RelativeLayout) findViewById(R.id.newgame);
p1_cards = (LinearLayout) findViewById(R.id.p1_cards);
mCards = new ArrayList<>();
center_deck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(total_cards>0) {
int pick1 = r.NextInt(52);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1f);
lp.setMargins(50,50,50,50);
ImageView cardImage = new ImageView(NewGame.this);
cardImage.setTag(mCards.get(pick1).getFilename());
cardImage.setLayoutParams(lp);
p1_cards.addView(cardImage);
int p1_current_card = getResources().getIdentifier(mCards.get(pick1).getFilename(),"drawable",getPackageName());
cardImage.setImageResource(p1_current_card);
total_cards--;
}
else
{
Snackbar.make(mRelativeLayout,"NO MORE CARDS",Snackbar.LENGTH_LONG).show();
}//if total cards > 0
}
});//button
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/newgame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/troll"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="50dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/center_deck"
android:layout_width="125dp"
android:layout_height="match_parent"
android:layout_marginLeft="130dp"
android:scaleType="centerInside"
android:src="@drawable/gray_back"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/deck_count"
android:layout_width="35dp"
android:layout_height="35dp"
android:text="52"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:textColor="#FFF"
android:gravity="center"
android:textSize="20sp"
android:background="#666"
tools:ignore="ContentDescription" />
</LinearLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:id="@+id/p1_cards"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>