[英]How to Create Square shape programmatically in android?
我正在以编程方式在LinearLayout中制作Button。 请看下面的代码:
My LinearLayout in my .xml file :
<LinearLayout
android:id="@+id/outerRelativeLayout_relativeLayout2_making_dynamically"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
我以编程方式使按钮像:
LinearLayout outerRelativeLayout = (LinearLayout) findViewById(R.id.outerRelativeLayout_relativeLayout2_making_dynamically);
Button imgBtn = new Button(MyActivity.this);
imgBtn.setId(Integer.parseInt(c.getString(0)));
LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
imageViewParams.setMargins(0, 10, 10, 10);
outerRelativeLayout.addView(imgBtn, imageViewParams);
这里c.getString(0);
表示值来自本地数据库,例如1,2,3 ...
此配置文件看起来像附加的图片:
在上面的图像中,橙色是LinearLayout,而倒数是通过编程方式创建的按钮。
现在我想做的是:单击任何圆形按钮后,应在该按钮上创建一个白色正方形,就像要提及的是选择了哪个按钮。,我了解View
但我不明白如何使用它来实现我想要的。 有人可以告诉我该怎么做吗? 希望我能很好地解释我的问题。
例如:当单击“第三个按钮”(在绿色圆圈a中)...时,应该看起来像此图像表示现在已选择了绿色圆形按钮:
一种解决方案是:
您可以在按钮上显示两个图像,即普通图像和Onclick图像,而不用编程方式进行。
因此,单击该按钮时,图像会更改。
例如:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/buttonclick" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/buttonclick" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/button"/>
</selector>
要不然:
您只需要以编程方式更改坐标即可使其成为正方形。
但是我更喜欢使用第一个解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.