Android / Layout - 实用主要活动布局

时间:2013-09-16 09:08:50

标签: android android-layout

我正在学习如何逐步构建我需要开发的应用程序的布局。 我已经在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无法强制转换为android.widget.RelativeLayout $ LayoutParams

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

由于

1 个答案:

答案 0 :(得分:1)

视图的布局参数必须与其父视图兼容。在您的情况下,View中有一个RelativeLayout,但您提供的是LinearLayout.LayoutParams ClassCastException

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

import android.widget.LinearLayout.LayoutParams;

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

import android.widget.RelativeLayout;

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

RelativeLayout.LayoutParams

从其他LayoutParams获得资格。