充气使用XML风格按钮 [英] inflate a button using xml to style

查看:100
本文介绍了充气使用XML风格按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图夸大一个按钮

code:

 的LinearLayout LL =(的LinearLayout)this.getLayoutInflater()膨胀(R.layout.assets,NULL);
按钮BTN =(按钮)ll.getChildAt(0);

R.layout.assets(assets.xml):

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:ID =@ + ID /主
    机器人:layout_height =match_parent
    机器人:weightSum =1
    机器人:填充=50像素>
    <按钮的android:文本=按钮的android:layout_width =WRAP_CONTENT机器人:ID =@ + ID / myButton的机器人:layout_height =WRAP_CONTENT>< /按钮>
< / LinearLayout中>

异常(堆栈跟踪):

  08-01 21:45:48.631:ERROR / AndroidRuntime(12406):致命异常:主要
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):了java.lang.RuntimeException:无法启动活动ComponentInfo {} com.foxbusiness.tv/com.foxbusiness.tv.FOXBusinessActivity:java.lang.IllegalStateException:本指定的子已经有一个父。您必须先对孩子的父母打电话removeView()。
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.app.ActivityThread.access $ 1500(ActivityThread.java:124)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1016)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.os.Handler.dispatchMessage(Handler.java:99)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.os.Looper.loop(Looper.java:132)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.app.ActivityThread.main(ActivityThread.java:4083)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在java.lang.reflect.Method.invokeNative(本机方法)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在java.lang.reflect.Method.invoke(Method.java:491)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在dalvik.system.NativeStart.main(本机方法)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):java.lang.IllegalStateException:产生的原因指定的小孩已经有一个父。您必须先对孩子的父母打电话removeView()。
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.view.ViewGroup.addViewInner(ViewGroup.java:3145)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.view.ViewGroup.addView(ViewGroup.java:3034)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.view.ViewGroup.addView(ViewGroup.java:2991)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.view.ViewGroup.addView(ViewGroup.java:2971)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在com.foxbusiness.tv.FOXBusinessActivity.onCreate(FOXBusinessActivity.java:112)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1732)
08-01 21:45:48.631:ERROR / AndroidRuntime(12406):... 11个


解决方案

我有同样的问题。看看我的问题关于使用充气<从样式XML添加对象/ A>

答案涉及以下code:

 的LinearLayout视图=(的LinearLayout)LayoutInflater.from(本).inflate(R.layout.my_button,NULL);
//或的LinearLayout buttonView =(的LinearLayout)this.getLayoutInflater()膨胀(R.layout.my_button,NULL);
按钮则myButton =(按钮)view.findViewById(R.id.myButton);
view.removeView(myButton的);MAINVIEW的LinearLayout =(的LinearLayout)this.findViewById(R.id.mainLayout);
mainView.addView(myButton的);

Trying to inflate a Button

code:

LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.assets, null);
Button btn = (Button)ll.getChildAt(0);

R.layout.assets (assets.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:id="@+id/main" 
    android:layout_height="match_parent" 
    android:weightSum="1" 
    android:padding="50px">
    <Button android:text="Button" android:layout_width="wrap_content" android:id="@+id/myButton" android:layout_height="wrap_content"></Button>
</LinearLayout>

exception (stack trace):

08-01 21:45:48.631: ERROR/AndroidRuntime(12406): FATAL EXCEPTION: main
08-01 21:45:48.631: ERROR/AndroidRuntime(12406): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.foxbusiness.tv/com.foxbusiness.tv.FOXBusinessActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.ActivityThread.access$1500(ActivityThread.java:124)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1016)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.os.Looper.loop(Looper.java:132)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.ActivityThread.main(ActivityThread.java:4083)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at java.lang.reflect.Method.invoke(Method.java:491)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at dalvik.system.NativeStart.main(Native Method)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3145)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.view.ViewGroup.addView(ViewGroup.java:3034)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.view.ViewGroup.addView(ViewGroup.java:2991)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.view.ViewGroup.addView(ViewGroup.java:2971)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at com.foxbusiness.tv.FOXBusinessActivity.onCreate(FOXBusinessActivity.java:112)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1732)
08-01 21:45:48.631: ERROR/AndroidRuntime(12406):     ... 11 more

解决方案

I was having the same issue. Take a look at my question regarding adding an object from the style xml using the inflater.

The answer involved the following code:

LinearLayout view = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.my_button, null);
// or LinearLayout buttonView = (LinearLayout)this.getLayoutInflater().inflate(R.layout.my_button, null);
Button myButton = (Button) view.findViewById(R.id.myButton);
view.removeView(myButton);

LinearLayout mainView = (LinearLayout)this.findViewById(R.id.mainLayout);
mainView.addView(myButton);

这篇关于充气使用XML风格按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆