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

发布于 2024-12-16 14:40:14 字数 2022 浏览 1 评论 0原文

有没有办法使用 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”),但这种情况永远不会发生。

谁能帮我解决这个问题吗?

Is there any way to adjust height and width of a custom component using XML attributes?

For example, I created a component and its layout.

Here is XML layout of the component:

<?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>

And here is a simple class that inflates this layout:

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);
    }
}

The custom component is used in layout of some activity that looks something like this:

<?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>

The component is meant to be stretched to full size of a screen (that's why its layout parameters set to "fill_parent") but that is never happen.

Can anyone help me with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随心而道 2024-12-23 14:40:14

由于您的 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 );
    }
}

Seeing as your CustomComponent extends LinearLayout, you just need to inflate the layout directly into it (parsing this as the ViewGroup, as your CustomComponent will be the holding view):

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

If that doens't work, try supplying only the constructor that takes a context:

public class CustomComponent extends LinearLayout
{
    public CustomComponent(Context context)
    {
        super(context);
        inflate( context, R.layout.custom_component, this );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文