将 XML 布局扩展为自定义 ViewGroup

发布于 2024-11-30 06:34:02 字数 2090 浏览 2 评论 0原文

我正在尝试将 xml 布局膨胀到自定义 ViewGroup 中。膨胀布局后,ViewGroup 仅显示布局的根视图,实际上它应该将完整的布局文件显示到 ViewGroup 中。

我已经浏览了 stackoverflow 和其他网站上发布的与此相关的其他类似问题,但没有任何帮助。

这是我的自定义 ViewGroup 的代码:

public class ViewEvent extends ViewGroup {
private final String LOG_TAG="Month View Event";
public ViewEvent(Context context) {
        super(context); 
    }

    public ViewEvent(Context context,AttributeSet attrs) {
        super(context,attrs);   
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {      

        int childCount= getChildCount();
        for(int i=0;i<childCount;i++)
        {   
            Log.i(LOG_TAG, "Child: " + i + ", Layout [l,r,t,b]: " + l + "," + r + "," + t + "," + b);
            View v=getChildAt(i);
            if(v instanceof LinearLayout)
            {
                Log.i(LOG_TAG, "Event child count: " + ((ViewGroup)v).getChildCount()); // displaying the child count 1
                v.layout(0, 0, r-l, b-t);
            }
            //v.layout(l, r, t, b);
        }
    }   

Activity 中,我使用此自定义视图组:

ViewEvent event = new ViewEvent(getApplicationContext());
LayoutInflater inflator;
inflator=(LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflator.inflate(R.layout.try1, event);
setContentView(event);

以下是布局文件:

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


    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffff0000"
        android:text="asdasadfas" />

</LinearLayout>

膨胀此布局后,我只得到根 LinearLayout 背景颜色为 #ffff0000TextView 未显示。

我哪里做错了或者遗漏了什么?

I am trying to inflate xml layout into custom ViewGroup. After inflating the layout, ViewGroup only displays the root view of the layout in-fact it should display the full layout file into the ViewGroup.

I have gone through the other similar questions posted related to this on stackoverflow and other websites but none helped.

Here is the code of my custom ViewGroup:

public class ViewEvent extends ViewGroup {
private final String LOG_TAG="Month View Event";
public ViewEvent(Context context) {
        super(context); 
    }

    public ViewEvent(Context context,AttributeSet attrs) {
        super(context,attrs);   
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {      

        int childCount= getChildCount();
        for(int i=0;i<childCount;i++)
        {   
            Log.i(LOG_TAG, "Child: " + i + ", Layout [l,r,t,b]: " + l + "," + r + "," + t + "," + b);
            View v=getChildAt(i);
            if(v instanceof LinearLayout)
            {
                Log.i(LOG_TAG, "Event child count: " + ((ViewGroup)v).getChildCount()); // displaying the child count 1
                v.layout(0, 0, r-l, b-t);
            }
            //v.layout(l, r, t, b);
        }
    }   

In an Activity, I am using this custom view group:

ViewEvent event = new ViewEvent(getApplicationContext());
LayoutInflater inflator;
inflator=(LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflator.inflate(R.layout.try1, event);
setContentView(event);

Following is the layout file:

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


    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffff0000"
        android:text="asdasadfas" />

</LinearLayout>

After inflating this layout, I am only getting the root LinearLayout with backgroud color #ffff0000. TextView is not shown up.

Where I am doing wrong or missing something?

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

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

发布评论

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

评论(2

抚你发端 2024-12-07 06:34:02

尝试事件的 getParent() (您的 ViewEvent):

inflator.inflate(R.layout.try1, event.getParent(), false);

try getParent() of event (your ViewEvent):

inflator.inflate(R.layout.try1, event.getParent(), false);
撩起发的微风 2024-12-07 06:34:02

尝试这样。您没有为 TextView 声明任何 id。

   <TextView 
            android:id="@+id/testTextId"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ffff0000"
            android:text="asdasadfas" />

然后像下面这样访问这个 TextView。

TextView testTextView = (TextView) ViewEvent.findViewById(R.id.testTextId);

Try like this.You did not declare any id for your TextView.

   <TextView 
            android:id="@+id/testTextId"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ffff0000"
            android:text="asdasadfas" />

Then access this TextView like following.

TextView testTextView = (TextView) ViewEvent.findViewById(R.id.testTextId);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文