我想动态创建Buttons并将它们放在布局上,但它给了我一些错误。我是第一次这样做,所以对这些事情的想法很少。
我的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>
下面是我从DB获取记录列表并在布局上放置这些按钮的类。我的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)
指导我,我做错了。我从数据库中获取正确的数据,它工作正常。任何帮助表示赞赏。
答案 0 :(得分:2)
如果你在一个地方添加按钮到相对布局,所有按钮都会创建。我的意思是第二个按钮将在第一个按钮创建。取LinearLayout而不是relativelayout。然后它会一个接一个地创建..或..根据您的方向。
答案 1 :(得分:2)
为View设置LayoutParams时,需要为其父视图定义LayoutParams对象。在这种情况下,您要为keyslayout1设置LayoutParams,其父级是ScrollView。所以你应该使用ViewGroup.LayoutParams(或适用于ScrollView的LayoutParams对象,无论哪个是正确的)而不是RelativeLayout.LayoutParams。