单击自定义 LinearLayout 中包含的按钮时出现意外行为

发布于 2024-10-30 13:05:54 字数 2247 浏览 4 评论 0原文

我为应用程序创建的自定义 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,

enter image description here

And it doesn't matter what item I presse I get this,

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灼痛 2024-11-06 13:05:54

答案是我无法重用列表中的可绘制对象。我需要为每个按钮创建一个可绘制对象,以解决问题。感谢 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.

ゝ杯具 2024-11-06 13:05:54

我不知道这是一个错误还是一个功能,但可以使用以下代码实现类似的行为;

    Drawable d = getResources().getDrawable(R.drawable.bg);
    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);
    button1.setBackgroundDrawable(d);
    button2.setBackgroundDrawable(d);        

这里,尽管单击了按钮 1,按钮 2 仍被绘制为“按下”,这似乎与将相同的可绘制对象指定为两个按钮的背景这一事实有关。您可以通过为每个按钮创建自己的背景 Drawable 来解决这个问题。

I don't know if this is a bug or a feature but similar behavior can be achieved with following code;

    Drawable d = getResources().getDrawable(R.drawable.bg);
    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);
    button1.setBackgroundDrawable(d);
    button2.setBackgroundDrawable(d);        

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.

冬天旳寂寞 2024-11-06 13:05:54

为什么不在循环内定义 template 呢?

if (items.size() > 0) {

        for (int i = 0; i < items.size(); i++) {

            View 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);
        }

    }

我希望这有助于

编辑

尝试将其用于您的选择器,并尝试将其添加到 XML 中。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused state -->
    <item android:state_focused="false" 
          android:state_pressed="false" 
          android:drawable="@drawable/button_phone"" />
    <!-- Focused state -->
    <item android:state_focused="true" 
          android:state_pressed="false" 
          android:drawable="@drawable/button_phone_over" />   
    <!-- Pressed state -->
    <item android:state_pressed="true" 
          android:drawable="@drawable/button_phone_over" />
 </selector>

我有一种感觉,问题在于您在代码中重用了可绘制对象,因此,如果您没有在 XML 中执行此操作,则为每个背景创建一个新的可绘制对象,而不是重用该背景。

Why dont you try defining template inside the loop?

if (items.size() > 0) {

        for (int i = 0; i < items.size(); i++) {

            View 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);
        }

    }

I hope this helps

EDIT

Try using this for your selector, and also try adding it in the XML.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused state -->
    <item android:state_focused="false" 
          android:state_pressed="false" 
          android:drawable="@drawable/button_phone"" />
    <!-- Focused state -->
    <item android:state_focused="true" 
          android:state_pressed="false" 
          android:drawable="@drawable/button_phone_over" />   
    <!-- Pressed state -->
    <item android:state_pressed="true" 
          android:drawable="@drawable/button_phone_over" />
 </selector>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文