自定义模块视图显示不正确

时间:2021-03-05 10:34:45

标签: android android-studio android-layout textview

我正在 android 中创建一个可搜索的微调器库,并在 xml 布局文件中使用该库,我使用 textView 来显示它。但是当我在 xml 文件中添加我的自定义视图时,它没有像我创建的那样显示。 请看下面的截图以正确理解。

my custom textView in android library

textView when I am using it in my activity layout file(textview is below button and it is also highlighted)

SpinnerView.java 类包含 TextView

public class SpinnerView extends RelativeLayout {

    private Context context;
    private AttributeSet attrs;
    private int styleAttr;
    private View view;

    TextView textView;

    boolean textAllCaps;
    String text;

    public SpinnerView(@NonNull Context context) {
        super(context);
        this.context = context;
        init();
    }

    public SpinnerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.attrs = attrs;
        init();
    }

    public SpinnerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        this.attrs = attrs;
        this.styleAttr = defStyleAttr;
        init();
    }

    private void init() {
        this.view = this;
        inflate(context,R.layout.spinner_view,null);

        TypedArray arr = context.obtainStyledAttributes(attrs,R.styleable.SpinnerView,styleAttr,0);

        textAllCaps = arr.getBoolean(R.styleable.SpinnerView_textAllCaps,false);
        text = arr.getString(R.styleable.SpinnerView_android_text);

        textView = findViewById(R.id.spinnerView);

        if(text != null) {
            textView.setText(text);
        }

        arr.recycle();
    }

    public boolean isTextAllCaps() {
        return textAllCaps;
    }

    public void setTextAllCaps(boolean textAllCaps) {
        textView.setAllCaps(textAllCaps);
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        textView.setText(text);
    }
}

spinner_view.xml-包含文本视图设计

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">


    <TextView
        android:id="@+id/spinnerView"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeightSmall"
        android:background="@drawable/round_spinner_view"
        android:textColor="@color/whiteblack"
        android:layout_marginHorizontal="10dp"
        android:padding="10dp"
        android:drawableRight="@drawable/ic_dropdown"/>

</RelativeLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">


    <Button
        android:id="@+id/show"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="show Dialog"/>


    <com.rex.spinlib.SpinnerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/show"
        android:layout_marginTop="24dp" />
</RelativeLayout>

如何在活动布局文件中显示我想要的视图?

0 个答案:

没有答案