【发布时间】:2014-07-30 23:32:28
【问题描述】:
我正在尝试以编程方式创建一个新的 ListView 并将自定义样式应用于:
// 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 没有问题,但未正确应用自定义样式。 android:layout_marginXY 属性无法识别,因此未正确放置 ListView。如果我使用 android:paddingX 代替一切正常。 为什么会这样?
如果我不以编程方式而是直接在 XML 中创建 ListView 并将样式应用于它,android:layout_marginXY 属性可以正常工作。
创建 ListView 与在 XML 或 Java 中应用样式有什么区别?
【问题讨论】:
标签: android android-layout android-listview android-styles