安卓:膨胀()忽略利用在样式表中定义的根布局宽度/高度。二进制XML错误 [英] Android : inflate() ignoring utilizing root layout width/height defined in style sheets. BInary XML error

查看:305
本文介绍了安卓:膨胀()忽略利用在样式表中定义的根布局宽度/高度。二进制XML错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目,我转换GUI到样式表。我使用的是布局,然后景观/写真集样式表。我用一个充气膨胀的布局,因为我动态添加和删除内容,并认为自身是动态由我的code插入应用程序创建和销毁。

I'm working on a project, and I'm converting the GUI over to Style sheets. I use a layout, and then a landscape/portrait set of style sheets. I use an inflator to inflate the layout, as I am dynamically adding and removing content, and the view itself is dynamically created and destroyed by an application which my code plugs into.

myView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.bearing_layout, parentView, false);

错误:了java.lang.RuntimeException:二进制XML文件,2号线:你必须提供一个layout_width属性。

Error: Java.Lang.RuntimeException: Binary XML file, line 2: You must supply a layout_width attribute.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/bearing_overall_layout">
    <LinearLayout 
        style = "@style/bearing_header_layout">
        <TextView android:id="@+id/header" 
            style = "@style/bearing_header"
            android:background="@drawable/transparentbackground"
            android:text="@string/bearing_title" />
    </LinearLayout>
    <RelativeLayout style="@style/BearingSelectListContainer"
        android:id="@+id/top_view">
        <LinearLayout 
            style="@style/BearingSelectList_linear_layout">
            <TextView style="@style/BearingSelectListTitle" 
                android:id="@+id/title"
                android:text="@string/bearing_title" />
            <ScrollView android:id="@+id/listScroller"
                style="@style/bearing_list_scroller">
                <LinearLayout
                    style="@style/bearing_scroll_list_linear_layout" >
                    <LinearLayout android:id="@+id/pointList"
                        style="@style/bearing_point_list">
                    </LinearLayout>
                </LinearLayout>
            </ScrollView>
        <LinearLayout 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_gravity="center_horizontal">
                <Button style="@style/BearingSelectListButton" android:id="@+id/view" android:text="@string/view_button" />
                <Button style="@style/BearingSelectListButton" android:id="@+id/my_loc" android:text="@string/my_loc_button" />
                <Button style="@style/BearingSelectListButton" android:id="@+id/center" android:text="@string/center_button" />
                <Button style="@style/BearingSelectListButton" android:id="@+id/exit" android:text="@string/exit_button" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

和样式表:

<resources>
<style name="bearing_overall_layout">
    <item name="android:orientation">vertical</item>
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">300dp</item>
</style>
<style name = "bearing_header_layout">
    <item name ="android:orientation">horizontal</item>
    <item name ="android:layout_width">fill_parent</item>
    <item name ="android:layout_height">wrap_content</item>
    <item name ="android:layout_alignParentTop">true</item>
    <item name ="android:gravity">center</item>
</style>
<style name="bearing_header">
    <item name = "android:padding">10dp</item>
    <item name = "android:textSize">18sp</item>
    <item name = "android:textStyle">bold</item>
    <item name = "android:layout_width">wrap_content</item>
    <item name = "android:layout_height">wrap_content</item>
    <item name = "android:layout_alignParentTop">true</item>
    <item name = "android:gravity">center</item>
</style>
<style name="GotoMgrsInput">
    <item name="android:textSize">18dp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">300dp</item>
    <item name="android:background">#ff000000</item>
</style>
    <style name="BearingSelectMgrsInput">
    <item name="android:textSize">16sp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_weight">1</item>
    <item name="android:typeface">monospace</item>
</style>
<style name="bearing_list_scroller">
    <item name="android:layout_width">fill_parent</item> 
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">1</item>
    <item name="android:gravity">center</item>

</style>
<style name = "bearing_scroll_list_linear_layout">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">fill_parent</item>
    <item name="android:orientation">vertical</item>
    <item name="android:layout_marginBottom">10dp</item>
    <item name="android:gravity">center</item>  
</style>
<style name = "bearing_point_list">
    <item name="android:layout_width">fill_parent</item> 
    <item name="android:layout_height">fill_parent</item>
    <item name="android:layout_marginRight">1dip</item>
    <item name="android:orientation">vertical</item>
    <item name="android:gravity">center</item>
</style>

<style name="bearing_BearingSelectListContainer">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">fill_parent</item>
    <item name="android:background">#a000</item>
    <item name="android:layout_alignParentLeft">true</item>
    <item name="android:id">@+id/top_view</item>
    <item name="android:gravity">center</item>
    <item name="android:layout_alignParentLeft">false</item>
</style>
<style name="BearingSelectList_linear_layout">
    <item name="android:orientation">vertical</item>
    <item name="android:layout_height">fill_parent</item> 
    <item name="android:layout_width">fill_parent</item>

</style>
<style name="BearingSelectListTitle">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_gravity">center</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">24sp</item>
</style>    
<style name="bearing_BearingSelectListButton">
    <item name="android:layout_width">80dp</item>
    <item name="android:layout_height">wrap_content</item>
</style>
<style name="bearing_text_label">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_gravity">center</item> 
    <item name="android:shadowColor">#ff000000</item> 
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item> 
    <item name="android:shadowRadius">1</item> 
    <item name="android:textStyle">bold</item>
    <item name="android:clickable">false</item> 
    <item name="android:longClickable">false</item>
</style>
<style name="bearing_item_inner_layout">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:gravity">center</item>
</style>
<style name="bearing_specific_view_layout">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:orientation">horizontal</item>
    <item name="android:layout_alignParentLeft">true</item>
</style> 

现在,如果我很难定义的android:宽度和android:高度在基地布局一切完美运行。这是不是真的可以接受,因为我需要不同的价值观进行布局VS画像,以及为建立不同的手机。

Now if I hard define the android:width and android:height in the base layout everything else runs perfectly. This isn't really acceptable because I need different values for layout vs portrait, as well as for different builds of phones.

下一个选项是设置充气的rootView为null,这确实让布局PARAMS权(有点...),但是这会导致空指针,当我尝试和返回视图的访问内部元件。

The next option is to set the rootView of inflate to null, which does get the layout params right (sort of...), BUT this causes null pointers when I try and access internal elements of the view which is returned.

在我看来,像充气只是忽略了充气视图的风格元素,并从这个问题:

It seems to me like inflate is just ignoring the style elements of the inflated view, and from this question:

制作LayoutInflater感

我明白为什么,但同样这不我好在这种情况下,我已经尝试建议的方法(见上文),并使用样式表时,我仍然得到这个错误。

I see why, but again this doesn't do me any good in this case as I've attempted the suggested approaches (see above) and I am still getting this error when using style sheets.

我没有尝试过的唯一选项是硬编码值到布局,然后编程检测屏幕的方向和改变布局相应的参数,可以但这又需要为各种手机的建立做并且似乎打败样式表的全部目的。

The only option I haven't tried is hard coding the values into the layout, and then programatically detecting the orientation of the screen and altering the layout params accordingly, but again this would need to be done for a variety of phone builds and seems to defeat the entire purpose of style sheets.

任何人有什么想法?

推荐答案

好吧,

所以,我没有得到任何答复,这里是我的周围的工作:

So I've not gotten any responses, here is my work around:

更改此:

myView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.bearing_layout, parentView, false);

要这样:

myView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.bearing_layout, null, false);

这将正确加载你的资源的第一次。注意:这不会检测对方向改变你的最低水平视图布局的变化,但所有后续视图应该很好地工作。为了解决这个问题,我们添加一个辅助函数:

This will properly load your resources the FIRST TIME. Note: This WILL not detect layout changes for your lowest level view on orientation changes, though all subsequent view should work fine. To handle this we add a helper function:

genLayoutParams(yourView);

private void genLayoutParams(View reuse) { // apply whatever layout parameters you see fit, these are just examples.
    if (activity.getResources().getConfiguration().orientation == activity.getResources().getConfiguration().ORIENTATION_LANDSCAPE) {
        reuse.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.FILL_PARENT));

    }
    else {
        if (android.os.Build.VERSION.SDK_INT >= 13) {
            reuse.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    250));
        }

        else{
            reuse.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    300));
        }
    }
}

希望可以节省有人为有些无奈。

Hope that saves somone some frustration.

这篇关于安卓:膨胀()忽略利用在样式表中定义的根布局宽度/高度。二进制XML错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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