0

我想动态创建按钮并将它们放在布局上,但这给了我一些错误。我是第一次这样做,所以对这些事情的了解很少。

我的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:id="@+id/parentRelativeLayout"

            <LinearLayout
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_height="fill_parent"
                    android:layout_width="fill_parent"
                    android:orientation="vertical"
                    android:id="@+id/deptKeyslayout">
     <com.hellovx100.in.HorizontalPager
        android:id="@+id/horizontal_pager"
        android:layout_width="fill_parent"
        android:layout_height="0px"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="310dp"
        android:layout_weight="1">
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/keyslayout1">
            </RelativeLayout>

        </ScrollView>
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/keyslayout2">
            </RelativeLayout>

        </ScrollView>
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/keyslayout3">
            </RelativeLayout>

        </ScrollView>
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/keyslayout4">
            </RelativeLayout>

        </ScrollView>
    </com.hellovx100.in.HorizontalPager>
        </LinearLayout>


   <!--  <include layout="@layout/keys_layout"
                 android:layout_toRightOf="@+id/subtotalbox"/> -->

</RelativeLayout>

下面是我从数据库中获取记录列表并将这些按钮放在布局上的类。我的 MainClass.java

package com.hellovx100.in;

import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.Log;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

public class MainSaleScreen extends Activity{
    private HorizontalPager mPager;
    private RadioGroup mRadioGroup;
    Cursor mCursor;
    DatabaseHandler db;
    RelativeLayout parentLayout;
    RelativeLayout keysLayout1;
    RelativeLayout keysLayout2;
    RelativeLayout keysLayout3;
    RelativeLayout keysLayout4;
    com.hellovx100.in.HorizontalPager horizontalPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_sale_screen);
        parentLayout=(RelativeLayout)findViewById(R.id.parentRelativeLayout);
        keysLayout1=(RelativeLayout)findViewById(R.id.keyslayout1);
        keysLayout2=(RelativeLayout)findViewById(R.id.keyslayout2);
        keysLayout3=(RelativeLayout)findViewById(R.id.keyslayout3);
        keysLayout4=(RelativeLayout)findViewById(R.id.keyslayout4);
        DigitalClock dc = (DigitalClock) findViewById(R.id.digitalClock1);
         mPager = (HorizontalPager) findViewById(R.id.horizontal_pager);
         mPager.setOnScreenSwitchListener(onScreenSwitchListener);
         db=new DatabaseHandler(this);
         try{
             // Reading all contacts
            Log.d("Reading: ", "Reading all contacts..");
            List<Contact> contacts = db.getAllContacts();  
          final Button dept[]=new Button[100];
         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
            for (Contact cn : contacts) {
                int id=cn.getID();
                String deptName=cn.getName();
                if(id==1){
                    dept[id]=new Button(this);
                    dept[id].setText(deptName);
                    dept[id].setId(id);
                    keysLayout1.setLayoutParams(params);
                      keysLayout1.addView(dept[id]);
                                        }
                else if(id>=2 && id<=7 ){
                    dept[id]=new Button(this);
                    dept[id].setText(deptName);
                    dept[id].setId(id);
                    params.addRule(RelativeLayout.RIGHT_OF,dept[id].getId()-1);
                    keysLayout1.setLayoutParams(params);
                    keysLayout1.addView(dept[id]);
        }
                else if(id==8){
                    dept[id]=new Button(this);
                    dept[id].setText(deptName);
                    dept[id].setId(id);
                    keysLayout2.addView(dept[id]);
        }
                else if(id>=9 && id<=14){
                    dept[id]=new Button(this);
                    dept[id].setText(deptName);
                    dept[id].setId(id);
                    params.addRule(RelativeLayout.RIGHT_OF,dept[id].getId()-1);
                    keysLayout2.setLayoutParams(params);
                    keysLayout2.addView(dept[id]);
        }
                else if(id==15){
                    dept[id]=new Button(this);
                    dept[id].setText(deptName);
                    dept[id].setId(id);
                    keysLayout3.addView(dept[id]);
                    Log.d("I am in 5th if", ""+deptName);
                }
                else if(id>=16 && id<=21){
                    dept[id]=new Button(this);
                    dept[id].setText(deptName);
                    dept[id].setId(id);
                    keysLayout3.setLayoutParams(params);
                    params.addRule(RelativeLayout.RIGHT_OF,dept[id].getId()-1);
                    keysLayout3.addView(dept[id]);
                    Log.d("I am in 6th if", ""+deptName);
                }
                else{
                    Log.d("I am in else", ""+id);
                    String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
                    // Writing Contacts to log
                    Log.d("Name: ", log);
                }


            }

         }
         catch(Exception e){
            e.printStackTrace();
         }


     }
     private final HorizontalPager.OnScreenSwitchListener onScreenSwitchListener =
         new HorizontalPager.OnScreenSwitchListener() {
             @Override
             public void onScreenSwitched(final int screen) {
                 // Check the appropriate button when the user swipes screens.
                 switch (screen) {
                     case 0:
                        mPager.setCurrentScreen(0, true);
                         break;
                     case 1:
                        mPager.setCurrentScreen(1, true);
                       break;
                     case 2:
                        mPager.setCurrentScreen(2, true);
                        break;
                     default:
                         break;
                 }
             }
         };

     }

现在Logcat在下面

01-30 15:08:51.970: E/AndroidRuntime(274): FATAL EXCEPTION: main
01-30 15:08:51.970: E/AndroidRuntime(274): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.FrameLayout.onLayout(FrameLayout.java:288)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.ScrollView.onLayout(ScrollView.java:1205)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at com.hellovx100.in.HorizontalPager.onLayout(HorizontalPager.java:198)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.View.layout(View.java:7035)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.os.Looper.loop(Looper.java:123)
01-30 15:08:51.970: E/AndroidRuntime(274):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-30 15:08:51.970: E/AndroidRuntime(274):  at java.lang.reflect.Method.invokeNative(Native Method)
01-30 15:08:51.970: E/AndroidRuntime(274):  at java.lang.reflect.Method.invoke(Method.java:521)
01-30 15:08:51.970: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-30 15:08:51.970: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-30 15:08:51.970: E/AndroidRuntime(274):  at dalvik.system.NativeStart.main(Native Method)

指导我在哪里做错了。我从数据库中获取了正确的数据,并且工作正常。任何帮助表示赞赏。

4

2 回答 2

2

如果您将按钮添加到一个地方的相对布局,所有按钮都将创建。我的意思是第二个按钮将在第一个按钮上创建。采用 LinearLayout 而不是 relativelayout。然后它将一个一个地创建..或..并排。根据你的方向。

于 2013-01-30T10:09:08.060 回答
2

在为 View 设置 LayoutParams 时,需要为其父视图定义 LayoutParams 对象。在这种情况下,您正在为父级为 ScrollView 的 keyslayout1 设置 LayoutParams。因此,您应该使用 ViewGroup.LayoutParams(或适用于 ScrollView 的 LayoutParams 对象,以正确者为准)而不是 RelativeLayout.LayoutParams。

于 2013-01-30T10:40:32.087 回答