【发布时间】:2016-09-23 02:05:32
【问题描述】:
我正在编写一个带有一些自定义视图的通用列表视图。没什么特别的。
但是当我在我的 Galaxy s7 中运行它时,在自定义项目视图的中心创建了一个神秘的水平间距。
这是自定义项目视图的抽象版本,它将在 listView baseAdapter 的 getView() 中返回。
public class CustomView extends LinearLayout {
public CustomView(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.cell_dutch, this, true);
}
}
添加 setLayoutParams(new LinearLayout.LayoutParams(..., ...)); 等代码没有效果。
布局如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/linearLayoutTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="3dp"
android:gravity="bottom"
android:orientation="vertical">
<TextView
android:id="@+id/textViewUnRead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginBottom="5dp"
android:text=""
android:textColor="@color/colorUnread"
android:textSize="9dp" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="5dp"
android:text="PM 3:11"
android:textColor="@color/color_chatcrey"
android:textSize="9dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<LinearLayout
android:background="@drawable/img_chat_bubble_white_you_02_android"
android:id="@+id/llBubble"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@drawable/..."
android:id="@+id/imageView" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/..." />
<!-- RED BOX -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOTAL"
android:textSize="10dp" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<TextView
android:id="@+id/textViewTotalPoint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="right"
android:textColor="#000000"
android:textSize="8dp"
android:textStyle="bold" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@drawable/..." />
</LinearLayout>
</LinearLayout>
<!-- PURPLE BOX -->
<ImageButton
android:id="@+id/imageButtonSnsShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/..." />
</LinearLayout>
</LinearLayout>
在上面的截图中,红色框和紫色按钮之间应该没有间距。实际上,在 android studio ui 编辑器上,我看不到任何间距,但它只是在运行时显示。
但在我为测试它而制作的另一个项目中,它看起来就像下图一样。
在两个项目之间将自定义视图显示为 listView 项的逻辑似乎没有区别。为什么会发生这种情况?
【问题讨论】:
-
这些年来,我已经看到了很多与三星设备相关的问题。如果您还没有,请尝试明确设置应用程序主题。三星设备的默认设置通常有点随意。
-
我假设您在
ListView中只有一种视图类型。您能否在您的布局文件中指出哪个部分属于 RED 和 PURPLE? -
不,实际上,我的视图中有超过五种视图类型。上面的代码是一个缩短版本,因为上面屏幕的结果来自那个源。我将在示例代码中添加一个标题来指示红色和紫色视图。
标签: android android-layout listview