我正在尝试动态添加图像,因为imageview源可以更改,图像计数也可以更改。
public class MatchingGame extends AppCompatActivity {
String TAG = "Chic";
RelativeLayout rLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "matchinggame OnCreate");
super.onCreate(savedInstanceState);
rLayout = new RelativeLayout(this);
setContentView(rLayout);
Log.d(TAG, "mx");
// rLayout = (RelativeLayout) findViewById(R.id.relative_layout);
RelativeLayout.LayoutParams rLayParams =
new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
rLayParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.CENTER_IN_PARENT);
Log.d(TAG, "mxx");
rLayout.setLayoutParams(rLayParams);
Log.d(TAG, "mmx");
ImageView img1 = new ImageView(this);
img1.setImageResource(R.drawable.ic_launcher);
img1.setTag(1);
Log.d(TAG, "qx");
rLayout.addView(img1);
Log.d(TAG, "mqqx");
rLayParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.CENTER_IN_PARENT);
// rLayout.setLayoutParams(rLayParams);
ImageView img2 = new ImageView(this);
img1.setImageResource(R.drawable.b3_1);
img1.setTag(2);
rLayout.addView(img2);
}
}
但我收到了这个错误:
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
我猜这是关于这条线
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
我需要将左侧中心放在一个图像上,而另一侧放置其他图像,这样我就需要对齐父级和大小规则。
但它会给出错误。
我有一个布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/relative_layout"
tools:context=".games.MatchingGame">
</RelativeLayout>
我需要这种布局吗?因为我已经动态创建了布局。
我也尝试过这个静态布局,尝试使用findviewbyid
,但总是犯同样的错误。
我可以看到没有setlayoutparams
方法的图片。但当然,没有任何规则,所以所有图像都在左上角。
答案 0 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "matchinggame OnCreate");
super.onCreate(savedInstanceState);
rLayout = new RelativeLayout(this);
setContentView(rLayout);
Log.d(TAG, "mx");
// rLayout = (RelativeLayout) findViewById(R.id.relative_layout);
rLayout.setBackgroundColor(Color.RED);
RelativeLayout.LayoutParams rLayParams = new RelativeLayout.LayoutParams(140,140);
RelativeLayout.LayoutParams rLayParams1 = new RelativeLayout.LayoutParams(140,140);
RelativeLayout.LayoutParams rLayParams2 = new RelativeLayout.LayoutParams(140,140);
rLayParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);
// rLayout.setLayoutParams(rLayParams);
ImageView img1 = new ImageView(this);
img1.setImageResource(R.drawable.ic_launcher);
img1.setTag(1);
rLayout.addView(img1,rLayParams);
//todo burda for olacak ona gore rlayparams degisecek
rLayParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rLayParams1.addRule(RelativeLayout.CENTER_IN_PARENT);
// rLayout.setLayoutParams(rLayParams);
ImageView img2 = new ImageView(this);
img2.setImageResource(R.drawable.b3_1);
img2.setTag(2);
int i=2;
img2.setId(R.id.img11);
rLayout.addView(img2,rLayParams1);
rLayParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rLayParams2.addRule(RelativeLayout.BELOW,R.id.img11);
// rLayout.setLayoutParams(rLayParams);
ImageView img3 = new ImageView(this);
img3.setImageResource(R.drawable.b3_1);
img3.setTag(3);
rLayout.addView(img3,rLayParams2);
}
我解决了这个问题。为每个图像创建不同的relativelayoutparams(因为它们有不同的规则)和
而不是
// rLayout.setLayoutParams(rLayParams);
我创建了这个
rLayout.addView(img1,rLayParams);
并且有效。