android 自定义属性文字颜色

cwtwac6a  于 2023-02-27  发布在  Android
关注(0)|答案(2)|浏览(107)

大家好,我是新来的。抱歉,如果我问一些简单的东西。我想改变自定义属性字符串的文本颜色。
attr.xml

<declare-styleable name="StoreCustomView">
        <attr name="strLimit" format="integer" />
        <attr name="strHeader" format="string" />
        <attr name="storeItemHeight" format="dimension" />
        <attr name="storeItemWidth" format="dimension" />
        <attr name="strLoader" format="boolean" />
    </declare-styleable>

片段_home

<com.test.apps.myapp.customView.StoreCustomView
                        android:id="@+id/horizontalStoreList"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:visibility="visible"
                        custom:storeItemHeight="@dimen/v3_store_card_height"
                        custom:storeItemWidth="@dimen/v3_store_card_width"
                        custom:strHeader="@string/discover_stores_nearby"
                        custom:strLimit="6" />

因此,在本例中,我希望更改strHeader文本颜色
先谢谢你
我已经尝试了android:文本颜色="",android:颜色=""

nzkunb0c

nzkunb0c1#

很抱歉缺乏信息。我不能很好地理解每个元素,但我希望它会帮助你,如果我张贴完整的代码StoreCustomView

package com.test.apps.myapp.customView;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.util.AttributeSet;

import com.test.apps.myapp.network.api_request.ApiRequest;
import com.test.apps.myapp.network.api_request.ApiRequestListeners;
import com.test.apps.myapp.parser.Parser;
import com.test.apps.myapp.utils.NSLog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.test.apps.myapp.R;
import com.test.apps.myapp.activities.StoreDetailActivity;
import com.test.apps.myapp.animation.ImageLoaderAnimation;
import com.test.apps.myapp.appconfig.Constances;
import com.test.apps.myapp.classes.Store;
import com.test.apps.myapp.controllers.stores.StoreController;
import com.test.apps.myapp.parser.api_parser.StoreParser;
import com.test.apps.myapp.views.HorizontalView;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import io.realm.RealmList;

import static com.test.apps.myapp.appconfig.AppConfig.APP_DEBUG;

public class StoreCardCustomView extends HorizontalView {

    private Context mContext;
    private Store mStore;
    private Map<String, Object> optionalParams;
    private View mView;
    private StoreListener sl;
    private int store_id = -1;

    public StoreCardCustomView(Context context) {
        super(context);
        mContext = context;
        setUIComponenet();
    }

    public StoreCardCustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;

        setCustomAttribute(context, attrs);

        setUIComponenet();
    }

    public void loadData(int store_id, boolean fromDatabase) {

        this.store_id = store_id;
        if (!fromDatabase) {
            loadDataFromAPi(store_id);
        }

    }

    public void loadData(int store_id, boolean fromDatabase, StoreListener sl) {

        this.sl = sl;
        this.store_id = store_id;

        if (!fromDatabase) {
            loadDataFromAPi(store_id);
        }

    }

    private void loadDataFromAPi(final int store_id) {

        findViewById(R.id.placeholder).setVisibility(VISIBLE);
        findViewById(R.id.content).setVisibility(GONE);

        Map<String, String> params = new HashMap<String, String>();
        params.put("limit", "1");
        params.put("store_id", String.valueOf(store_id));

        ApiRequest.newPostInstance(Constances.API.API_USER_GET_STORES, new ApiRequestListeners() {
            @Override
            public void onSuccess(Parser parser) {
                parse_data(parser);
            }

            @Override
            public void onFail(Map<String, String> errors) {
                findViewById(R.id.placeholder).setVisibility(VISIBLE);
                findViewById(R.id.content).setVisibility(GONE);
            }
        },params);

    }

    private void parse_data(Parser parser) {

        final StoreParser mStoreParser = new StoreParser(parser);
        RealmList<Store> list = mStoreParser.getStore();

        if (list.size() > 0) {

            StoreController.insertStores(list);
            mStore = list.get(0);
            setupComponent(mStore);

            if (sl != null) {
                sl.onLoaded(mStore);
            }

        } else {
            //hide item
            findViewById(R.id.cv_store_item).setVisibility(GONE);
        }

    }

    private void setUIComponenet() {

        setOrientation(LinearLayout.HORIZONTAL);
        setGravity(Gravity.CENTER_HORIZONTAL);

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.v2_card_item_store, this, true);

        mView = getChildAt(0);

        mView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClicked();
            }
        });

    }

    public void itemClicked() {

        if (APP_DEBUG)
            NSLog.e("_1_store_id", String.valueOf(store_id));

        Intent intent = new Intent(mContext, StoreDetailActivity.class);
        intent.putExtra("id", store_id);
        mContext.startActivity(intent);

    }

    private void setCustomAttribute(Context context, @Nullable AttributeSet attrs) {

        optionalParams = new HashMap<>();
        //get the attributes specified in attrs.xml using the name we included
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.StoreCustomView, 0, 0);

        try {
            //get the text and colors specified using the names in attrs.xml
            optionalParams.put("strLimit", a.getInteger(R.styleable.StoreCustomView_strLimit, 30));

        } finally {
            a.recycle();
        }
    }

    public void setupComponent(Store mStore) {
        if (mStore != null) {
            //fixing issue related to random redirection
            store_id = mStore.getId();

            if (mStore.getListImages() != null && mStore.getListImages().size() > 0) {

                Glide.with(mContext.getApplicationContext())
                        .load(mStore.getListImages().get(0)
                                .getUrl200_200())
                        .centerCrop().placeholder(ImageLoaderAnimation.glideLoader(mContext))
                        .into((ImageView) mView.findViewById(R.id.image));

            } else {

                Glide.with(mContext.getApplicationContext())
                        .load(R.drawable.def_logo)
                        .centerCrop().placeholder(R.drawable.def_logo)
                        .into((ImageView) mView.findViewById(R.id.image));

            }
            ((TextView) mView.findViewById(R.id.address)).setText(mStore.getAddress());
            double votes = mStore.getVotes();
            DecimalFormat decim = new DecimalFormat("#.##");
            ((TextView) mView.findViewById(R.id.rate)).setText(decim.format(votes) + "  (" + mStore.getNbr_votes() + ")");
            ((TextView) mView.findViewById(R.id.name)).setText(mStore.getName());

            updateCategoryBadge(mStore.getCategory_name(), mStore.getCategory_color());

            findViewById(R.id.placeholder).setVisibility(GONE);
            findViewById(R.id.content).setVisibility(VISIBLE);

        }
    }

    private void updateCategoryBadge(String title, String color_hex) {

        TextView catView = mView.findViewById(R.id.store_tag_category);

        catView.setVisibility(View.VISIBLE);

        int color = ContextCompat.getColor(mContext, R.color.colorPrimary);

        try {
            if (color_hex != null && !color_hex.equals("null"))
                color = Color.parseColor(color_hex);
        } catch (Exception e) {
            NSLog.e("colorParser", e.getMessage());
        }

        Drawable badge_cat_background = catView.getBackground();
        if (badge_cat_background instanceof ShapeDrawable) {
            ((ShapeDrawable) badge_cat_background).getPaint().setColor(color);
        } else if (badge_cat_background instanceof GradientDrawable) {
            ((GradientDrawable) badge_cat_background).setColor(color);
        } else if (badge_cat_background instanceof ColorDrawable) {
            ((ColorDrawable) badge_cat_background).setColor(color);
        }

        catView.setText(title);

    }

    public void setStoreListener(StoreListener l) {
        sl = l;
    }

    public interface StoreListener {
        void onLoaded(Store object);
    }

}
wn9m85ua

wn9m85ua2#

<com.test.apps.myapp.customView.StoreCustomView
    android:id="@+id/horizontalStoreList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible"
    custom:storeItemHeight="@dimen/v3_store_card_height"
    custom:storeItemWidth="@dimen/v3_store_card_width"
    custom:strHeader="@string/discover_stores_nearby"
    custom:strLimit="6"
    android:textColor="#FF0000" />

相关问题