【问题标题】:inflate in merge合并膨胀
【发布时间】:2012-05-09 09:26:27
【问题描述】:

您好,我正在尝试使用 <merge> 标签来扩展我的布局,这是我的 main.xml:

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

    <com.osmgames.kartuves.LinesLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/lines" />

</merge>

这是我的 LinesLayout.java:

public class LinesLayout extends FrameLayout {

    public LinesLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        super.onInterceptTouchEvent(event);
        return false;
    }
}

还有我的 main.java:

public class main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }}

所以我想将下面的 XML 膨胀为 main.xml。我该怎么做?

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

    <ImageView   
         android:layout_width="40dp" 
         android:layout_height="40dp"
         android:layout_gravity="center" 
         android:layout_marginTop="75dp" 
         android:layout_marginRight="40dp"
         android:src="@drawable/line" />

    <ImageView   
         android:layout_width="40dp" 
         android:layout_height="40dp"
         android:layout_gravity="center" 
         android:layout_marginTop="75dp" 
         android:src="@drawable/line" />

    <ImageView   
         android:layout_width="40dp" 
         android:layout_height="40dp"
         android:layout_gravity="center" 
         android:layout_marginTop="75dp" 
         android:layout_marginLeft="40dp"
         android:src="@drawable/line"  />
</FrameLayout>

这是我得到的日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.osmgames.kartuves/com.osmgames.kartuves.InGame}: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

Caused by: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at **com.osmgames.kartuves.InGame.onCreate(InGame.java:59)**
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more

InGame.java:59 是:

58 FrameLayout item = (FrameLayout)findViewById(R.id.lines);
59 View child = getLayoutInflater().inflate(R.layout.letters3, null);
60 item.addView(child);

【问题讨论】:

    标签: android layout merge frame inflate


    【解决方案1】:

    merge 标签应该用在膨胀的布局中,它会在父 ViewGroup 中合并自身。

    试试这个:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent">
    
        <com.osmgames.kartuves.LinesLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/lines" />
    
    </FrameLayout >
    

    还有这个:

    <?xml version="1.0" encoding="utf-8"?>
    <merge xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ImageView   
             android:layout_width="40dp" 
             android:layout_height="40dp"
             android:layout_gravity="center" 
             android:layout_marginTop="75dp" 
             android:layout_marginRight="40dp"
             android:src="@drawable/line" />
    
        <ImageView   
             android:layout_width="40dp" 
             android:layout_height="40dp"
             android:layout_gravity="center" 
             android:layout_marginTop="75dp" 
             android:src="@drawable/line" />
    
        <ImageView   
             android:layout_width="40dp" 
             android:layout_height="40dp"
             android:layout_gravity="center" 
             android:layout_marginTop="75dp" 
             android:layout_marginLeft="40dp"
             android:src="@drawable/line" />
    </merge>
    

    希望对你有帮助

    【讨论】:

    • Arf 抱歉,忘了一件事:您的合并视图需要有一个视图组作为合并视图。您也可以使用包含标签,可能会更好地满足您的需求。
    • 我尝试了包含,但我需要根据应用程序的需要以编程方式设置包含的布局。我需要设置应该插入的 8 个布局中的 1 个...
    • 有一点是肯定的:合并标签不应该在 main.xml 布局文件中。它应该在膨胀的视图中。膨胀的视图还必须有合并标签,然后是 viewGroup,然后是你的图像视图。
    • 刚刚看到了一些东西:FrameLayout item = (FrameLayout)findViewById(R.id.lines); View child = getLayoutInflater().inflate(R.layout.letters3, item );
    【解决方案2】:

    使用

        View headerView = View.inflate(this, R.layout.layout_name, null);
        lv.addHeaderView(headerView);
    

    为了让你的布局膨胀,它会给你你的父布局,其中所有视图都存在,然后对它进行类型转换,或者如果你想添加,然后将它直接添加到另一个布局。

    【讨论】:

    • 如果您还希望附加组件,也许您必须在最后一个参数中输入 true。
    • 如果您添加更多关于为什么必须这样做的解释可能会很好。
    【解决方案3】:

    你应该更好地使用

    58 FrameLayout item = (FrameLayout)findViewById(R.id.lines);
    59 View child = getLayoutInflater().inflate(R.layout.letters3, item, false);
    60 item.addView(child);
    

    或 你可以使用

    58 FrameLayout item = (FrameLayout)findViewById(R.id.lines);
        59 View child = getLayoutInflater().inflate(R.layout.letters3, item, true);
    

    当你传递 true 时,它​​会自动为你做这些事情。

    这将是一个少一个视图,而不是你添加的那个无用的 FrameLayout 来代替合并标签。

    【讨论】:

      猜你喜欢
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多