6

既存のビューにカスタムXML属性を追加しようとしています

それらをカスタムビューに追加することは大したことではありませんが、「基本的な」ビュー(TextView、LinearLayout、ImageViewなど)でこれらの属性にアクセスする方法がわかりません。

そして、フラグメントやライブラリプロジェクトが関係している場合、これはより困難になります

これまでのところ、これが私のコードです

税関属性の定義とXML(attrs.xmlとレイアウト):

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="StarWars">
    <attr name="jedi" format="string" />
    <attr name="rank" format="string" />
</declare-styleable>

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:sw="http://schemas.android.com/apk/res-auto"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="28dp"
    android:gravity="center_horizontal"
    sw:jedi="Obiwan" />

<TextView
    android:id="@+id/rank"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="28dp"
    android:gravity="center_horizontal"
    sw:rank="Master" />

これをフラグメントで膨らませるので(attrs argでonCreateを使用しないでください!)、2つのswカスタム属性を取得する唯一の方法は次のとおりです。

  1. カスタムLayoutInflater.FactoryをonCreateViewのフラグメントLayoutInflaterに設定します

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      super.onCreateView(inflater, container, savedInstanceState);
    
      LayoutInflater layoutInflater = inflater.cloneInContext(inflater.getContext());
      layoutInflater.setFactory(new StarWarsLayoutFactory());
    
      View fragmentView = layoutInflater.inflate(R.layout.jedi, container, false);
      ((TextView) fragmentView.findViewById(android.R.id.jedi)).setText("Yep");
    
      return fragmentView;
    }
    
  2. カスタムLayoutInflater.Factoryでカスタム属性を取得しようとしています:

    public class StarWarsLayoutFactory implements Factory {
      @Override
      public View onCreateView(String name, Context context, AttributeSet attrs) {
                  *** What to do here ? ***
    
          return null;
      }
    }
    

誰かがこの種の質問をしましたか?

私がここで欠けているものは何ですか?

事前にThx!

4

2 に答える 2

2

私はついにこれをしました:)

OPで行ったように新しいものを作成する必要がありますLayoutInflater.Factoryが、ファクトリはすべてのインフレーションされたレイアウトビューに使用され、Factory.onCreateView(Androidにインフレーションを処理させるために)nullを返す必要があるため、カスタムXML属性をどこかにキャッシュする必要があります

だからここに解決策:

  • レイアウトXMLビューにはandroid:idが必要です

  • カスタム属性を保持するクラスを作成します。

    public class AttributeParser {

    private AttributeParserFactory mFactory;
    private Map<Integer, HashMap<Integer, String>> mAttributeList;

    private class AttributeParserFactory implements LayoutInflater.Factory{
        @Override
        public View onCreateView(String name, Context context, AttributeSet attrs) {
            String id = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "id");

            if(id != null){
                // String with the reference character "@", so we strip it to keep only the reference
                id = id.replace("@", "");

                TypedArray libraryStyledAttributeList = context.obtainStyledAttributes(attrs, R.styleable.NewsHubLibrary);
                HashMap<Integer, String> libraryViewAttribute = new HashMap<Integer, String>();
                int i = 0;

                for(int attribute : R.styleable.NewsHubLibrary){
                    String attributeValue = libraryStyledAttributeList.getString(i);

                    if(attributeValue != null)
                        libraryViewAttribute.put(attribute, attributeValue);

                    i++;
                }

                if(!libraryViewAttribute.isEmpty())
                    mAttributeList.put(Integer.valueOf(id), libraryViewAttribute);

                libraryStyledAttributeList.recycle();
            }

            return null;
        }

    }

    public AttributeParser(){
        mAttributeList = new HashMap<Integer, HashMap<Integer, String>>();
        mFactory = new AttributeParserFactory();
    }

    public void clear() {
        mAttributeList.clear();
    }

    public LayoutInflater getLayoutInflater(LayoutInflater inflater) {
        clear();
        LayoutInflater layoutInflater = inflater.cloneInContext(inflater.getContext());
        layoutInflater.setFactory(mFactory);

        return layoutInflater;
    }

    public void setFactory(LayoutInflater inflater){
        inflater.cloneInContext(inflater.getContext()).setFactory(mFactory);
    }

    public void setViewAttribute(Activity activity) {
        for(Entry<Integer, HashMap<Integer, String>> attribute : mAttributeList.entrySet())
            if(activity.findViewById((Integer) attribute.getKey()) != null)
                activity.findViewById((Integer) attribute.getKey()).setTag(attribute.getValue());

    }

    public void setViewAttribute(View view) {
        for(Entry<Integer, HashMap<Integer, String>> attribute : mAttributeList.entrySet())
            if(view.findViewById((Integer) attribute.getKey()) != null)
                view.findViewById((Integer) attribute.getKey()).setTag(attribute.getValue());
    }

    public Map<Integer, HashMap<Integer, String>> getAttributeList() {
        return mAttributeList;
    }

    public void setAttributeList(Map<Integer, HashMap<Integer, String>> attributeList) {
        this.mAttributeList = attributeList;
    }
    }
  • AttributeParserを使用すると、カスタム属性が各Viewタグに保存されます。

    LayoutInflater layoutInflater = mAttributeParser.getLayoutInflater(inflater);
    View view = layoutInflater.inflate(R.layout.jedi, null);
    mAttributeParser.setViewAttribute(view);
于 2013-02-19T16:37:09.240 に答える
0

標準TextViewなどはこれらの属性にアクセスしません。ただし、それを拡張して、それらのカスタム属性の読み取りを含む機能を提供することができます。

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StarWars, 0, 0);

String jedi = a.getText(R.styleable.StarWars_jedi);
String rank = a.getText(R.styleable.StarWars_rank);

a.recycle();

常に最後に電話recycle()することを忘れないでください。

于 2013-02-10T17:58:00.780 に答える