java - Android ListView 行布局忽略填充

标签 java android listview android-layout baseadapter

我确定这是我忽略的简单问题,但我无法弄清楚。我有一个使用具有简单布局的自定义适配器的 ListView,我的填充值似乎被忽略了。这是我想要的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/Widget.Layout.ComponentEntry" android:padding="@dimen/preferred_padding">
    <include layout="@layout/component_entry_summary" />
</LinearLayout>

但是填充被忽略了。如果我使用嵌套的 LinearLayout 那么它可以工作,但内部 LinearLayout 似乎毫无意义。这有效:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/Widget.Layout.ComponentEntry">
    <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:padding="@dimen/preferred_padding">
        <include layout="@layout/component_entry_summary"  />
    </LinearLayout>
</LinearLayout>

第一个布局在 ListView 之外工作,所以我很困惑。此处供引用的是所使用的样式(还指定了填充):

<style name="Widget">
    <item name="android:textAppearance">@style/TextAppearance</item>
</style>

<style name="Widget.Layout">
    <!-- Empty Style -->
</style>

<style name="Widget.Layout.ListViewItem">
    <item name="android:background">@drawable/listview_item_background</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:padding">@dimen/preferred_padding</item>
</style>

<style name="Widget.Layout.ComponentEntry" parent="Widget.Layout.ListViewItem">
    <item name="android:layout_height">@dimen/listview_item_height</item>
</style>

以及包含的布局:

<merge xmlns:android="http://schemas.android.com/apk/res/android" >
    <ImageView android:id="@+id/iconImage"
               android:layout_gravity="center_vertical"
               android:layout_height="@dimen/icon_size"
               android:layout_width="@dimen/icon_size"
               android:scaleType="fitCenter"
               android:src="@drawable/ic_launcher" />

    <LinearLayout android:layout_gravity="center_vertical"
                  android:layout_height="wrap_content"
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:paddingLeft="@dimen/preferred_padding"
                  android:orientation="vertical">

        <TextView android:id="@+id/topText"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:paddingLeft="@dimen/preferred_padding"
                  android:singleLine="true"
                  android:text="@string/app_name"
                  android:textColor="@color/header_text_enabled"
                  android:textStyle="bold" />

        <TextView android:id="@+id/bottomText"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:ellipsize="marquee"
                  android:marqueeRepeatLimit="marquee_forever"
                  android:paddingLeft="@dimen/preferred_padding"
                  android:singleLine="true"
                  android:text="@string/app_name"
                  android:textColor="@color/header_text_enabled" />
    </LinearLayout>
</merge>

布局是这样膨胀的(我最初提供了一个 null parent 并认为这是问题所在,但它似乎不是:

public View getView(int position, View view, ViewGroup parent) {
    ViewHolder viewHolder;

    if (view == null) {
        view = _layoutInflater.inflate(_layoutResourceId, parent, false);
        ...

最佳答案

我遇到了同样的问题,当我删除代码以设置填充工作的背景时。

编辑:找出导致它的原因,它是 9 个补丁图像中的填充线,我想如果你将它留在绘图之外,它会使用布局 xml 中的填充。但实际上它随后将可拉伸(stretch)区域也用作可绘制区域。

If the padding lines are not included, Android uses the left and top lines to define this drawable area, effectively defining the padding.

http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

关于java - Android ListView 行布局忽略填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10392688/

相关文章:

listview - Flutter ListView在每次setState()调用时都跳到顶部

android - 当我生成上下文菜单时在 ListView 中选择一个项目

Debian 9 openjdk-7-jre 和 openjdk-7-jre-headless

java 。比较网页结构 (dom) 的相似性。

java - 非法状态异常 : Fragment already added

android 应用程序 socket.io 未连接到 node.js 服务器,但 android 浏览器建立连接

android - Listview 不在 android 中显示正确的图像

java - 用于调用重写特定方法的特定类的设计模式

Java 从控制台读取粘贴的输入,然后停止

java - 获取联系方式失败?