且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

布局未在android自定义组件中夸大

更新时间:2023-02-01 22:42:12

在您的XML文件中,您尝试使用自定义视图类(com.example.views.MyView),同时尝试在其中添加TextView .这是不可能的.

In you XML file you are trying to use a custom view class (com.example.views.MyView) and in the same time trying to add a TextView inside. It's not possible.

这是您需要更改的内容:

Here is what you need to change:

您必须在代码中添加XML文件:

You must inflate XML file in the code:

public MyView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    LayoutInflater.from(context).inflate(R.layout.<your_layout>.xml, this);
}

并按如下所示修改XML布局文件:

And modify the XML layout file like this:

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

<TextView
    android:id="@+id/text"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:ellipsize="end"
    android:maxLines="4"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:text="Some text" />

</LinearLayout>