Android:无法将背景可绘制颜色设置为 percentRelativeLayout

标签 android android-layout android-percentrelativelayout

我在 PercentRelativeLayout 中使用 View 标签。我需要为我无法以编程方式执行的相应标记设置背景颜色。

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/leaderboardRowRelativeLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent">

    <View
        android:id="@+id/percentile_scored"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="15%"
        />

    <View
        android:id="@+id/percentile_left_scoring"
    android:layout_width="0dp"
    android:layout_height="0dp"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="15%"
        android:layout_toRightOf="@+id/percentile_scored"
    />

我尝试设置背景的地方:

  if(String.valueOf(userId).equals(leaderBoardDb.getUserId())){
                leaderboardFriendsViewHolder.percent_scored.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.leaderboard_user_color));

            }else {
                leaderboardFriendsViewHolder.percent_scored.setBackground(ContextCompat.getDrawable(getApplicationContext(),R.drawable.leaderboard_row_color));
            }

最佳答案

好吧,我试过和你一样的代码。

1) 在设计中:

<android.support.percent.PercentRelativeLayout
    android:id="@+id/leaderboardRowRelativeLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/percentile_scored"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="50%"
        />

    <View
        android:id="@+id/percentile_left_scoring"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_toRightOf="@+id/percentile_scored"
        app:layout_heightPercent="15%"
        app:layout_widthPercent="50%"
        />
</android.support.percent.PercentRelativeLayout>

2) 在 RecyclerAdapter 中:

...
@Override
    public void onBindViewHolder(ProductImagesRecyclerAdapter.ViewHolder holder, int position) {
        Timber.e("Position: " + position);
        if(position%2 == 0) {
            holder.aa.setBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent));
        } else {
            holder.aa.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));
        }
    }
...

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView productImage;
        int position;
        View aa;


        public ViewHolder(View v, final ProductImagesRecyclerInterface productImagesRecyclerInterface) {
            super(v);
            aa = v.findViewById(R.id.percentile_scored);
        }

        public void setPosition(int position) {
            this.position = position;
        }

    }

结果似乎完全没问题: enter image description here

问题可能出在其他地方。可能 View 的宽度和高度为零,或者某些其他 View 与它们重叠。

关于Android:无法将背景可绘制颜色设置为 percentRelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35556790/

相关文章:

android - 如何将客户端 ID 添加到 Google Drive Client API for Java

android - 无法通过蓝牙调试 android wear - 无法连接到 localhost

android - 您可以使用 webview 在同一应用中打开横幅或插页式广告网址吗?

Android 防止双击 Spinner

java - 如何以编程方式隐藏 android 一些但不是全部的 XML 输出中的查看项目?

java - 以编程方式设置 PercentageRelativeLayout 中的宽高比

java - 以编程方式更改导航栏方向 (RTL)

android - MVVM:从内部 fragment 更新viewpager2的容器 View

android - 如何在 API 25 中使用 PercentRelativeLayout

Android - 将按钮放在 ImageView 上的确切位置