单击自定义 LinearLayout 中包含的按钮时出现意外行为
我为应用程序创建的自定义 LinearLayout 有问题。基本上,LinearLayout 包含项目,每个项目都是一个水平 LinearLayout,其中包含一个 TextView 和一个 Button。 LinearLayout 已正确填充,但是,当我按下按钮(其背景为自定义背景:选择器)时,会发生意外行为。发生的情况是按钮仅在 LinearLayout 的最后一个元素上更改外观,这没有任何意义。
填充 LinearLayout 的代码如下:
View template = null;
if (items.size() > 0) {
for (int i = 0; i < items.size(); i++) {
template = inflate(getContext(), R.layout.views_custom_list_item, null);
template.setBackgroundDrawable(getProperBackgroundDrawable(i, items.size() - 1));
TextView text = (TextView)template.findViewById(R.id.custom_list_text);
text.setText(items.get(i));
Button button = (Button)template.findViewById(R.id.custom_list_button);
button.setId(i);
button.setBackgroundDrawable(backgroundButton);
button.setOnClickListener(callListener);
addView(template);
template = null;
}
}
我正在扩充一个 XML,其中包含每个项目的布局,然后相应地设置属性。所有这些代码都包含在继承自 LinearLayout 的自定义类中。你知道问题出在哪里吗?谢谢
这是我的观点,
无论我按下哪个项目,我都会得到这个,
是项目 XML:
`
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/custom_list_text"
android:textSize="15sp"
android:textColor="@color/black"
android:paddingLeft="5dip" android:paddingRight="5dip"
android:layout_weight="1"
android:gravity="left" />
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/custom_list_button"/>
这
主布局上的 XML:
I have a problem with a custom LinearLayout I'm creating for an application. Basically, the LinearLayout contain items, each item is a horizontal LinearLayout which contains a TextView and a Button. The LinearLayout is populated correctly, however, when I press the button (which has a background a custom background: selector) unexpected behaviour is happening. What's happening is that the button changes is appearance only on the last element of the LinearLayout, which doesn't make any sense.
The code that populates the LinearLayout is as follows:
View template = null;
if (items.size() > 0) {
for (int i = 0; i < items.size(); i++) {
template = inflate(getContext(), R.layout.views_custom_list_item, null);
template.setBackgroundDrawable(getProperBackgroundDrawable(i, items.size() - 1));
TextView text = (TextView)template.findViewById(R.id.custom_list_text);
text.setText(items.get(i));
Button button = (Button)template.findViewById(R.id.custom_list_button);
button.setId(i);
button.setBackgroundDrawable(backgroundButton);
button.setOnClickListener(callListener);
addView(template);
template = null;
}
}
I'm inflating an XML which contains the layout for each item, and then I set the properties accordingly. All that code is contained in a custom class which inherits from LinearLayout. Do you know where the problem is? thanks
This is my view,
And it doesn't matter what item I presse I get this,
This is the item XML:
`
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/custom_list_text"
android:textSize="15sp"
android:textColor="@color/black"
android:paddingLeft="5dip" android:paddingRight="5dip"
android:layout_weight="1"
android:gravity="left" />
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/custom_list_button"/>
`
The XML on the main layout:
<com.example.views.CustomLayoutView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/custom_phone_directory"
android:layout_margin="5dip"
xx:background_single="@drawable/button_complete"
xx:background_top="@drawable/button_top"
xx:background_middle="@drawable/button_middle"
xx:background_bottom="@drawable/button_bottom"
xx:button_background="@drawable/button_phone_selector" />
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案是我无法重用列表中的可绘制对象。我需要为每个按钮创建一个可绘制对象,以解决问题。感谢 raukodraug 的所有支持,以及其他向我指出一些问题的人。
The answer was that I can't reuse a drawable object within the list. I need to create a drawable for each of the buttons, that solves the problem. Thanks to raukodraug for all his support, and to the others who point me some issues.
我不知道这是一个错误还是一个功能,但可以使用以下代码实现类似的行为;
这里,尽管单击了按钮 1,按钮 2 仍被绘制为“按下”,这似乎与将相同的可绘制对象指定为两个按钮的背景这一事实有关。您可以通过为每个按钮创建自己的背景 Drawable 来解决这个问题。
I don't know if this is a bug or a feature but similar behavior can be achieved with following code;
Here button2 is drawn as 'pressed' despite clicking button1 and it seems to relate to fact same drawable is assigned as a background for both buttons. You can get around this by creating own background Drawable for each button.
为什么不在循环内定义
template
呢?我希望这有助于
编辑
尝试将其用于您的选择器,并尝试将其添加到 XML 中。
我有一种感觉,问题在于您在代码中重用了可绘制对象,因此,如果您没有在 XML 中执行此操作,则为每个背景创建一个新的可绘制对象,而不是重用该背景。
Why dont you try defining
template
inside the loop?I hope this helps
EDIT
Try using this for your selector, and also try adding it in the XML.
I have a feeling that the problem is that you are reusing the drawable in the code, so if you are not doing it in the XML then create a new drawable for each background, instead of reusing the one.