这就是我在编码中做的事情,我想在按钮点击时创建控件。用户将单击的次数我要添加控件的次数相同的次数。我必须在相对布局中添加这些控件。我已经在xml布局中创建了它,其中已经有一组控件。如果用户想要编辑,并想让它为更多的控制工作。
View DynamicView= new View(this);
DynamicView.setId(123);
DynamicView.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT))
以下是XML布局代码。
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
问题:- 1)不想以这种方式提供ids :(
DynamicView.setId(123);
我想分配这样的ids,我们可以在xml布局中分配
android:id="@+id/spnrIngredients1"
2)如何在编码中为相对布局赋值,如
android:layout_toRightOf="@id/tvIngredientsName"
android:layout_toLeftOf="@id/tvIngredientsName"
android:layout_below="@id/tvIngredientsName"
android:layout_above="@id/tvIngredientsName"
3)如何为背景颜色赋值。它不接受十六进制代码或其他东西。它要求的是哪种类型的int值?
发布于 2012-03-28 06:45:54
2)使用LayoutParams
设置在RelativeLayout
中放置视图的规则
Button b = new Button(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, viewID);
b.setLayoutParams(lp);
b.setText("Added at Bottom");
mParent.addView(b, lp);
上面的代码将把id为viewID
的Button
放在视图下面。
3) setBackgroundColor()
(我想这就是你正在使用的方法)需要一个代表Color
的整数,你可以这样设置它:
Color.RED
Color.parse(Color.parseColor("#0077cc"))
android.R.color.black
1)您可以在values/ids.xml
文件中设置您的ids,然后将它们设置为您的视图,然后通过这些ids引用视图:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="myfirstid" />
</resources>
现在您可以在代码中使用id R.id.myfirstid
(我不知道这是否是您想要的)。注意:我不知道这是否是推荐的。
https://stackoverflow.com/questions/9532220
复制相似问题