0

我正在以编程方式在 LinearLayout 中制作按钮。请看下面的代码:

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...

这个 lyout 看起来像附图: 在此处输入图像描述

在上图中,橙色是 LinearLayout,圆形是通过编程创建的按钮。

现在我想做:当点击任何圆形按钮时,应该在该按钮上创建一个白色方块,就像提到选择了哪个按钮一样。我知道View但我不明白如何使用它来实现我想要的。谁能告诉我该怎么做?希望我能很好地解释我的问题....

例如:当第三个按钮(在图像中的绿色圆形 a)单击时...应该看起来像此图像意味着现在选择了绿色圆形按钮:

在此处输入图像描述

4

1 回答 1

0

一种解决方案是:

您可以在按钮上放置两个图像,而不是以编程方式执行此操作,正常和 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>

要不然:

您只需以编程方式更改坐标即可使其成为正方形。

但我更喜欢使用第一个解决方案。

于 2013-09-25T12:12:56.860 回答