Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以编程方式创建自定义样式的ListView-为什么android:layout_marginXY无法识别?_Android_Android Layout_Android Listview_Android Styles - Fatal编程技术网

以编程方式创建自定义样式的ListView-为什么android:layout_marginXY无法识别?

以编程方式创建自定义样式的ListView-为什么android:layout_marginXY无法识别?,android,android-layout,android-listview,android-styles,Android,Android Layout,Android Listview,Android Styles,我正在尝试以编程方式创建新的ListView,并将自定义样式应用于is: // Somewhere within a Fragment... ListView myListView = new ListView(getActivity(), null, R.attr.myListViewStyle); someContainerView.addView(myListView); // In attrs.xml <?xml version="1.0" encoding="utf-8"?&g

我正在尝试以编程方式创建新的ListView,并将自定义样式应用于is:

// Somewhere within a Fragment...
ListView myListView = new ListView(getActivity(), null, R.attr.myListViewStyle);
someContainerView.addView(myListView);

// In attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="AppTheme">
        <attr name="myListViewStyle" format="reference"/>
    </declare-styleable>
</resources>

// in styles.xml
<style name="AppTheme" parent="AppBaseTheme">
    <item name="myListViewStyle">@style/ListViewStyle</item>
</style>

<style name="BookingsSectionListView" parent="@android:style/Widget.ListView">
    <!-- Setting the Padding DOES work... -->
    <item name="android:paddingLeft">50dp</item>

    <!-- ...while setting the Margin has NO effect -->
    <item name="android:layout_marginTop">50dp</item>  
</style>
//片段中的某个地方。。。
ListView myListView=新建ListView(getActivity(),null,R.attr.myListView样式);
someContainerView.addView(myListView);
//在attrs.xml中
//在styles.xml中
@样式/列表视图样式
50dp
50dp
虽然创建ListView没有问题,但自定义样式没有正确应用。android:layout_marginXY属性无法识别,因此ListView的位置不正确。如果我使用安卓:paddingX,一切正常这是为什么?

如果我不是以编程方式而是直接用XML创建ListView,并将样式应用于它,那么
android:layout_marginXY
属性将毫无问题地工作


创建ListView和在XML或Java中应用样式之间有什么区别?

您不应该将
布局*
属性视为子样式的一部分(
ListView
在本例中)。
布局*
属性位于添加子样式的父级上下文中。它们告诉父级在何处定位以及如何布局子级(子级没有这种逻辑)

在这方面:

ListView myListView = new ListView(getActivity(), null, R.attr.myListViewStyle);
所有属性都被传递到
列表视图
,样式将应用于
列表视图
本身。它没有任何父对象-它只是新创建的对象,尚未附加到视图层次结构

someContainerView.addView(myListView);

这是一行,其中
LayputParams
被分配给child。您没有指定任何
LayoutParams
(此方法有不同的版本),因此将使用默认版本。

非常感谢!这让事情更清楚了!