简体   繁体   English

如何避免ListView将行与TextView透明化

[英]How to avoid ListView put rows with TextView transparent

I'm developing a Chat App, so I create a list of friends. 我正在开发一个聊天应用程序,所以我创建了一个朋友列表。

Take a look at my ListView declaration at layout.xml 看看我在layout.xml上的ListView声明

<ListView
        android:listSelector="@android:color/transparent"
        android:id="@+id/usersList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:divider="@android:color/transparent"
        android:dividerPadding="2dp"
        android:overScrollFooter="@null"
        android:scrollingCache="false"
        android:transcriptMode="normal"
        />

And the behavior of this ListView is annoying. 这个ListView的行为很烦人。

When I run my app in a phone with Android 2.3 the TextView itens of my ListView becomes transparent just like the image below: 当我在装有Android 2.3的手机中运行我的应用程序时,ListView的TextView itens变得透明,如下图所示:

在此处输入图片说明

Sometimes when a press the itens the color of TextView back to normality. 有时,当按下按钮时,TextView的颜色恢复正常。

When I run my app in a phone with Android 4.1 this problem not happens. 当我在装有Android 4.1的手机中运行我的应用程序时,不会发生此问题。

在此处输入图片说明

My user item xml: 我的用户项目xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:padding="5dp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="65dp"
                android:layout_height="65dp"
                android:layout_gravity="center"
                android:layout_margin="5dp"
                android:adjustViewBounds="true"
                android:src="@drawable/quick_blox_user" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:orientation="vertical" >

                <TextView
                android:cacheColorHint="@android:color/transparent"
                android:id="@+id/text_view_user"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asdasdad"
                android:textColor="@color/text_gray"
                android:textSize="25sp" />

                <TextView 
                android:cacheColorHint="@android:color/transparent"
                android:id="@+id/text_view_user_last_activity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asdasdad"
                android:textColor="@color/text_gray_light"
                android:textSize="12sp" />

            <LinearLayout
                android:id="@+id/layout_pending_messages"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/button_message_pending_number"
                    android:focusable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="1dp"
                    android:background="@color/color_new_message_title"
                    android:padding="2dp"
                    android:text="12"
                    android:textColor="@android:color/white"
                    android:textSize="24sp" />

                <TextView
                    android:id="@+id/text_view_user_last_activity"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="1dp"
                    android:text="@string/pending_messages"
                    android:textColor="@color/color_new_message"
                    android:textSize="14sp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

My Adapter: 我的适配器:

public class UserAdapter extends BaseCustomAdapter<UserItem> {

    public UserAdapter(Activity a, List<UserItem> d) {
        super(a, d); 
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if(view == null) {
            view = LayoutInflater.from(activity).inflate( R.layout.user_quickblox_item, null);  
        }

        UserItem user  = data.get(position); 

        String userLogin = user.getUser().getLogin();
        int index = userLogin.indexOf("@");
        if(index != -1) {
            userLogin = userLogin.substring(0, index);
        }
        int count = ChatMessageDAO.getInstance(activity).countMessagePendentBySenderId(user.getUser().getId());

        View viewPendingMessages = view.findViewById(R.id.layout_pending_messages);
        if(count == 0) {
            viewPendingMessages.setVisibility(View.GONE);
        } else {
            viewPendingMessages.setVisibility(View.VISIBLE);
            Button buttonMessageNumberIndicator = (Button)view.findViewById(R.id.button_message_pending_number);
            buttonMessageNumberIndicator.setText(String.valueOf(count));
        }

        TextView textViewUser = (TextView)view.findViewById(R.id.text_view_user);
        textViewUser.setText(userLogin);

        TextView textViewUserLastActivity = (TextView)view.findViewById(R.id.text_view_user_last_activity);


        long diff = new Date().getTime() - user.getUser().getLastRequestAt().getTime();  
        long diffInDays =  TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
        if(diffInDays == 0) {
            long diffInHours =  TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
            if(diffInHours == 0) {
                long diffInMinutes =  TimeUnit.MINUTES.convert(diff, TimeUnit.MILLISECONDS);
                if(diffInMinutes < 0) {
                    textViewUserLastActivity.setText("Esta online agora");  
                }  else {
                    textViewUserLastActivity.setText("Entrou hoje " + diffInMinutes+ " minuto(s) atrás");   
                }
            } else {
                textViewUserLastActivity.setText("Entrou hoje " + diffInHours+ " horas atrás");
            }

        } else if(diffInDays == 1) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(user.getUser().getLastRequestAt());
            int hour = calendar.get(Calendar.HOUR_OF_DAY); 
            textViewUserLastActivity.setText("Entrou ontem as " + hour + " horas");  
        } else {
            textViewUserLastActivity.setText("Entrou " + diffInDays  + " dia(s) atras");    
        }

        return view;
    } 

}

How to avoid this problem? 如何避免这个问题?

There is a chance that the TextViews are not showing at all. TextViews有可能根本不显示。 not the colour is changing. 颜色没有改变。 you can check that by setting a background for the textviews. 您可以通过为textview设置背景来进行检查。

A few mistakes in the item xml I would like to point out: 我想指出xml项目中的一些错误:

  1. You have two items in the horizontal layout. 您在水平布局中有两个项目。 the linear layout that contains the image and the linear layout that contains textview. 包含图像的线性布局和包含textview的线性布局。 In this case you can't have them both match_parent. 在这种情况下,您不能将它们都设为match_parent。 the total size would be screen size * 2. 总尺寸为屏幕尺寸* 2。

  2. when setting weight for a linear layout in a horizontal container you set the width to 0dp. 在水平容器中为线性布局设置权重时,将宽度设置为0dp。

  3. your weight sum is 1 + 0.5 = 1.5. 您的体重总和是1 + 0.5 = 1.5。 means that the image will reserve 2/3 of screen size and textview 1/3. 表示图像将保留屏幕大小的2/3和textview的1/3。 looks un-logical. 看起来不合逻辑。

These un logical values could cause different layout display for different os version. 这些不合逻辑的值可能会导致不同的操作系统版本显示不同的布局。

Solution: 解:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:padding="5dp" >

        <LinearLayout
            **android:layout_width="0dp"**
            android:layout_height="match_parent"
            **android:layout_weight="1"**
            android:orientation="vertical" >

            <ImageView
                android:layout_width="65dp"
                android:layout_height="65dp"
                android:layout_gravity="center"
                android:layout_margin="5dp"
                android:adjustViewBounds="true"
                android:src="@drawable/quick_blox_user" />
        </LinearLayout>

        <LinearLayout
            **android:layout_width="0dp"**
            android:layout_height="match_parent"
            **android:layout_weight="2"**
            android:orientation="vertical" >

                <TextView
                android:cacheColorHint="@android:color/transparent"
                android:id="@+id/text_view_user"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asdasdad"
                android:textColor="@color/text_gray"
                android:textSize="25sp" />

                <TextView 
                android:cacheColorHint="@android:color/transparent"
                android:id="@+id/text_view_user_last_activity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asdasdad"
                android:textColor="@color/text_gray_light"
                android:textSize="12sp" />

            <LinearLayout
                android:id="@+id/layout_pending_messages"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/button_message_pending_number"
                    android:focusable="false"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="1dp"
                    android:background="@color/color_new_message_title"
                    android:padding="2dp"
                    android:text="12"
                    android:textColor="@android:color/white"
                    android:textSize="24sp" />

                <TextView
                    android:id="@+id/text_view_user_last_activity"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="1dp"
                    android:text="@string/pending_messages"
                    android:textColor="@color/color_new_message"
                    android:textSize="14sp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

What solved my problem was android:background : 解决我问题的是android:background

<LinearLayout              

android:background="@android:color/white android:background =“ @ android:color / white

            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:orientation="vertical" >

                <TextView
                android:cacheColorHint="@android:color/transparent"
                android:id="@+id/text_view_user"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asdasdad"
                android:textColor="@color/text_gray"
                android:textSize="25sp" />

                <TextView 
                android:cacheColorHint="@android:color/transparent"
                android:id="@+id/text_view_user_last_activity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="asdasdad"
                android:textColor="@color/text_gray_light"
                android:textSize="12sp" />

At my LinearLayout which has the TextViews at my user_item.xml(The layout of itens of the List). 在我的LinearLayout上,在我的user_item.xml(列表的itens的布局)中具有TextViews。 I put background color as White. 我将背景色设为白色。 Its strange because the main ViewGroup of this layout is already set with background color White. 这很奇怪,因为此布局的主ViewGroup已经设置为白色背景色。 Now My itens appears normally. 现在,我的项目将正常显示。

An Annoying solution for an Annoying problem. 一个烦人的问题的烦人的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM