0

我正在学习如何以编程方式为我需要开发的应用程序构建布局。我已经在 xml 文件中复制了正确的布局,现在我想以编程方式进行(它将变成动态的)。我有一些疑问需要澄清。所以,这里是xml代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20sp"
android:background="@drawable/background"
tools:context=".DiLand" >
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <RelativeLayout  
        android:layout_height="fill_parent"  
        android:layout_width="fill_parent"
        android:background="@color/grayColor"
        android:layout_marginTop="10sp" > 
       <TextView  
            android:text="1 copy"  
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"   
            android:gravity="center"  
            android:textColor="#000"     
            android:layout_centerHorizontal="true"  
            android:padding="25dp"/>
        <TextView  
            android:text="$20"  
            android:layout_height="wrap_content"    
            android:gravity="center"  
            android:textColor="#000"    
            android:layout_width="wrap_content"  
            android:layout_alignParentRight="true"  
            android:padding="25dp"/>    
    </RelativeLayout>
</LinearLayout>

这是我的java代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photo_management);

        //SCROLL VIEW
        ScrollView scrollView = new ScrollView(this);
        scrollView.setBackground(getResources().getDrawable(R.drawable.background));
        scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                                                     LayoutParams.MATCH_PARENT));
        scrollView.setPadding(20, 20, 20, 20);

        //LINEAR LAYOUT
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));

        //RELATIVE LAYOUT
        RelativeLayout relativeLayout = new RelativeLayout(this);
        relativeLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));
        //Need to understand how put a margin top to the relativeLayout

        //TEXT VIEWS
        TextView numberCopies = new TextView(this);
        numberCopies.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        numberCopies.setGravity(Gravity.CENTER);
        numberCopies.setPadding(25, 25, 25, 25);
        numberCopies.setTextColor(getResources().getColor(R.color.blackColor));
        numberCopies.setText("2 copies ");
        RelativeLayout.LayoutParams layoutParamsNumberCopies = (RelativeLayout.LayoutParams) numberCopies.getLayoutParams();
        layoutParamsNumberCopies.addRule(RelativeLayout.CENTER_HORIZONTAL);
        numberCopies.setLayoutParams(layoutParamsNumberCopies);

        TextView priceCopies = new TextView(this);
        priceCopies.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        priceCopies.setGravity(Gravity.CENTER);
        numberCopies.setPadding(25, 25, 25, 25);
        priceCopies.setTextColor(getResources().getColor(R.color.redColor));
        priceCopies.setText("$ 25 ");
        RelativeLayout.LayoutParams layoutParamsPriceCopies = (RelativeLayout.LayoutParams) priceCopies.getLayoutParams();
        layoutParamsPriceCopies.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        priceCopies.setLayoutParams(layoutParamsPriceCopies);

        scrollView.addView(scrollView);
        scrollView.addView(linearLayout);
        scrollView.addView(relativeLayout);    
    }
}

当我尝试启动活动时,程序崩溃并给我这种错误: - ClassCastException Android.widget.linearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

所以我需要了解我做错了什么以及我能做些什么来正确显示布局。这是我第一次尝试这样做,我在这里找到了一些关于 stackoverflow 的教程,可以帮助我更好地理解。但可能我缺少一些经验。

谢谢

4

1 回答 1

1

视图的布局参数必须与其父视图兼容。在您的情况下,您有一个View 内部 aRelativeLayout但您正在提供它,LinearLayout.LayoutParams它为您提供了ClassCastException.

在您的代码中,这些LayoutParams是不合格的,因此归结为您可能拥有的导入

import android.widget.LinearLayout.LayoutParams;

处理多种父布局时LayoutParams,最好只导入布局类

import android.widget.RelativeLayout;

然后在代码中而不是普通LayoutParams使用

RelativeLayout.LayoutParams

从 other 中限定它LayoutParams

于 2013-09-16T09:17:40.463 回答