Android RelativeLayout LayoutParams不能强制转换为FrameLayout LayoutParams
我正在尝试动态添加图像,因为imageview源是可变的,图像计数也是可变的Android RelativeLayout LayoutParams不能强制转换为FrameLayout LayoutParams,android,android-layout,Android,Android Layout,我正在尝试动态添加图像,因为imageview源是可变的,图像计数也是可变的 public class MatchingGame extends AppCompatActivity { String TAG = "Chic"; RelativeLayout rLayout; @Override protected void onCreate(Bundle savedInstanceState) { Log.d(TAG, "matchinggame
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>
我需要这个布局吗?因为我已经动态创建了布局
我也尝试过放置这个静态布局,尝试过使用findviewbyd
制作,但总是出现相同的错误
我可以看到没有setlayoutparams
方法的图像。但当然,没有任何规则,所以所有图像都在左上角
@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);
它成功了。检查其他帐户上的消息。