Android 安卓如何使用XML属性调整自定义组件的高度和宽度?

Android 安卓如何使用XML属性调整自定义组件的高度和宽度?,android,xml,components,height,width,Android,Xml,Components,Height,Width,是否有任何方法可以使用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

是否有任何方法可以使用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>
自定义组件用于某些活动的布局,如下所示:

<?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”),但这永远不会发生


有人能帮我吗?

鉴于您的
CustomComponent
扩展了
LinearLayout
,您只需将布局直接膨胀到其中(将
解析为视图组,将
CustomComponent
解析为保留视图):

如果这不起作用,请尝试仅提供接受上下文的构造函数:

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

鉴于您的
CustomComponent
扩展了
LinearLayout
,您只需将布局直接膨胀到其中(将
解析为视图组,将
CustomComponent
解析为保留视图):

如果这不起作用,请尝试仅提供接受上下文的构造函数:

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

第一个解决方案很好,但第二个不可能,因为当您在XML布局中添加视图时,它将搜索构造函数(上下文、属性集)。谢谢您的快速回答,您让我度过了美好的一天。第一个解决方案有效。第一个解决方案很好,但第二个不可能,因为当您在XML布局中添加视图时,它将搜索构造函数(上下文、属性集)。谢谢您的快速回答,您让我度过了美好的一天。第一种解决方案有效。
public class CustomComponent extends LinearLayout
{
    public CustomComponent(Context context)
    {
        super(context);
        inflate( context, R.layout.custom_component, this );
    }
}