Android. Как настроить высоту и ширину пользовательского компонента, используя атрибуты XML? - PullRequest
1 голос
/ 17 ноября 2011

Есть ли способ настроить высоту и ширину пользовательского компонента, используя атрибуты XML?

Например, я создал компонент и его макет.

Вот XML-макет компонента:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="TextView" />

</LinearLayout>

А вот простой класс, который раздувает этот макет:

public class CustomComponent extends LinearLayout
{
    public CustomComponent(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater)context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewGroup view = (ViewGroup)inflater.inflate(
            R.layout.custom_component, null);
        addView(view);
    }
}

Пользовательский компонент используется в макете некоторого действия, которое выглядит примерно так:

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

    <view
        class = "com.tests.CustomComponent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

Компонент должен быть растянут до полного размера экрана (поэтому его параметры макета установлены на "fill_parent"), но этого никогда не происходит.

Может кто-нибудь помочь мне с этим?

1 Ответ

1 голос
/ 17 ноября 2011

Видя, как ваш CustomComponent расширяется LinearLayout, вам просто нужно надуть макет прямо в него (парсинг this как ViewGroup, так как ваш CustomComponent будет удерживающим видом):

public class CustomComponent extends LinearLayout
{
    public CustomComponent(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        inflate( context, R.layout.custom_component, this );
    }
}

Если это не сработает, попробуйте указать только конструктор, который принимает контекст:

public class CustomComponent extends LinearLayout
{
    public CustomComponent(Context context)
    {
        super(context);
        inflate( context, R.layout.custom_component, this );
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...