Android自定义LinearLayout没有出现

时间:2016-10-27 17:18:28

标签: android view android-linearlayout

我在创建自定义LinearLayout

时遇到问题

我已在xml中定义了我的自定义布局,如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">

<TextView
    android:id="@+id/textViewIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="48sp"
    android:text="abc" />

<TextView
    android:id="@+id/textViewMessage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="24sp"
    android:text="abc" />

<Button
    android:id="@+id/btnAction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp" />
</LinearLayout>

然后我在attrs.xml中定义了一些属性

 <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <declare-styleable name="CustomEmptyListView">
        <attr name="visualIcon" format="string"/>
        <attr name="message" format="string"/>
        <attr name="textColor" format="color"/>
        <attr name="iconColor" format="color"/>
        <attr name="buttonColor" format="color"/>
        <attr name="buttonVisibility" format="boolean" />
        <attr name="buttonText" format="string" />
    </declare-styleable>
    </resources>

以下是我的自定义类

的代码
public class CustomEmptyListView extends LinearLayout {
    private TextView txtIcon;
    private TextView txtMessage;
    private Button btnAction;

    public CustomEmptyListView(Context context, AttributeSet attrs) {
        super(context, attrs);


    }

    public CustomEmptyListView(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.empty_listview, this);
    }

    public CustomEmptyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
        initViews(context, attrs);
    }


    private void initViews(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomEmptyListView, 0, 0);
        String iconText = a.getString(R.styleable.CustomEmptyListView_visualIcon);
        String message = a.getString(R.styleable.CustomEmptyListView_message);
        int buttonColor = a.getColor(R.styleable.CustomEmptyListView_buttonColor, ContextCompat.getColor(context,R.color.blueGreen));
        int textColor = a.getColor(R.styleable.CustomEmptyListView_textColor, ContextCompat.getColor(context,R.color.textCard));
        int iconColor = a.getColor(R.styleable.CustomEmptyListView_textColor, ContextCompat.getColor(context,R.color.light_grey));
        boolean btnVisible = a.getBoolean(R.styleable.CustomEmptyListView_buttonVisibility,true);
        String buttonText = a.getString(R.styleable.CustomEmptyListView_buttonText);
        a.recycle();

        LayoutInflater.from(getContext()).inflate(
                R.layout.empty_listview, this);

        txtIcon = (TextView)findViewById(R.id.textViewIcon);
        txtIcon.setText(iconText);
        txtIcon.setTextColor(iconColor);

        txtMessage = (TextView)findViewById(R.id.textViewMessage);
        txtMessage.setText(message);
        txtMessage.setTextColor(textColor);

        btnAction = (Button)findViewById(R.id.btnAction);
        btnAction.setText(buttonText);
        btnAction.setBackgroundColor(buttonColor);
        btnAction.setTextColor(ContextCompat.getColor(context,R.color.white));
        if(btnVisible){
            btnAction.setVisibility(View.GONE);
        }else{
            btnAction.setVisibility(View.VISIBLE);
        }
    }
}

将其添加到我的布局xml文件中,如下所示

<com.package.views.CustomEmptyListView
        android:id="@+id/emptyListView"
        android:visibility="visible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        custom:message="@string/no_hay_tareas_registradas"
        custom:visualIcon="@string/icon_tasks"
        android:layout_centerInParent="true"
        custom:buttonText="@string/anadir_tarea">

    </com.package.views.CustomEmptyListView>

在我的xml编辑器中,我只能看到0宽度和高度组件,并且在运行时没有显示。我做错了什么? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

放入inflate并在第一个构造函数中一起调用initViews。从inflate方法中删除第二个initViews。从第二个和第三个构造函数中删除它们。这应该做的事情。

我相信你有main_activity.xml布局,例如LinearLayout,其中包含CustomEmptyListView。布局empty_list_view包含2个TextViews和一个Button

答案 1 :(得分:0)

您必须为属性定义正确的命名空间。见post