Java 将自定义形状应用于android listview
您好,我正在尝试将自定义形状和视图应用到我的android listview。当有足够多的元素添加到listview中时,它可以正常工作,您必须滚动才能看到所有元素。但是如果少于这个,它看起来是这样的Java 将自定义形状应用于android listview,java,android,listview,Java,Android,Listview,您好,我正在尝试将自定义形状和视图应用到我的android listview。当有足够多的元素添加到listview中时,它可以正常工作,您必须滚动才能看到所有元素。但是如果少于这个,它看起来是这样的 这是我的抽屉里的形状 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient andr
这是我的抽屉里的形状
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#2ECC71"
android:endColor="#2ECC71"
android:angle="270"/>
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
下面是实现列表视图的代码
mListView = (ListView) findViewById(R.id.listView);
String[] listItems = new String[listOfUserIds.size()];
for(int i = 0; i < users.size(); i++){
listItems[i] = users.get(i);
}
ArrayAdapter adapter = new ArrayAdapter(EventDetailsActivity.this, android.R.layout.simple_list_item_1, listItems);
mListView.setAdapter(adapter);
mListView=(ListView)findViewById(R.id.ListView);
String[]listItems=新字符串[ListofUserId.size()];
对于(int i=0;i
下面是当有足够的元素滚动时的情况
您为列表视图提供了背景,列表视图的高度为
163dp
,因此如果只有几个项目,它将无法填充整个布局。所以你可以看到背景。解决方案不是给你的内容提供固定的高度,也不是给列表提供背景,而是给列表项提供背景。Muthukrishnan的回答在技术上是正确的,但它似乎并没有真正解决“我怎么能有一个圆角的列表视图”
”的问题
正如他所说,您应该从ListView
标记中删除背景,而是将背景应用于项目视图。如果你这样做,你要么(a)有一个正方形的列表视图,要么(b)每个项目都有圆角
不幸的是,如果不提供背景,就无法在列表视图中提供圆角
我能想到的最佳解决方案是在列表视图
上覆盖第二个视图
,并使用此视图绘制圆角。我用了一个矢量绘图来做这个
这里有一个非常简单的应用程序,可以在实际操作中显示这一点
活动\u main.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#777">
<FrameLayout
android:layout_width="333dp"
android:layout_height="163dp"
android:layout_gravity="center">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#00ffffff"
android:dividerHeight="10dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/fake_rounded_corners"/>
</FrameLayout>
</FrameLayout>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:gravity="center_vertical"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:background="#fff"/>
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="333dp"
android:height="163dp"
android:viewportWidth="333.0"
android:viewportHeight="163.0">
<path
android:fillColor="#777"
android:pathData="M0 7v-7h7a7 7 0 0 0 -7 7"/>
<path
android:fillColor="#777"
android:pathData="M326 0h7v7a7 7 0 0 0 -7 -7"/>
<path
android:fillColor="#777"
android:pathData="M333 156v7h-7a7 7 0 0 0 7 -7"/>
<path
android:fillColor="#777"
android:pathData="M0 156v7h7a7 7 0 0 1 -7 -7"/>
</vector>
itemview.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#777">
<FrameLayout
android:layout_width="333dp"
android:layout_height="163dp"
android:layout_gravity="center">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#00ffffff"
android:dividerHeight="10dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/fake_rounded_corners"/>
</FrameLayout>
</FrameLayout>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:gravity="center_vertical"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:background="#fff"/>
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="333dp"
android:height="163dp"
android:viewportWidth="333.0"
android:viewportHeight="163.0">
<path
android:fillColor="#777"
android:pathData="M0 7v-7h7a7 7 0 0 0 -7 7"/>
<path
android:fillColor="#777"
android:pathData="M326 0h7v7a7 7 0 0 0 -7 -7"/>
<path
android:fillColor="#777"
android:pathData="M333 156v7h-7a7 7 0 0 0 7 -7"/>
<path
android:fillColor="#777"
android:pathData="M0 156v7h7a7 7 0 0 1 -7 -7"/>
</vector>
假圆角.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#777">
<FrameLayout
android:layout_width="333dp"
android:layout_height="163dp"
android:layout_gravity="center">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#00ffffff"
android:dividerHeight="10dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/fake_rounded_corners"/>
</FrameLayout>
</FrameLayout>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:gravity="center_vertical"
android:textColor="@color/colorPrimary"
android:textSize="18sp"
android:background="#fff"/>
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="333dp"
android:height="163dp"
android:viewportWidth="333.0"
android:viewportHeight="163.0">
<path
android:fillColor="#777"
android:pathData="M0 7v-7h7a7 7 0 0 0 -7 7"/>
<path
android:fillColor="#777"
android:pathData="M326 0h7v7a7 7 0 0 0 -7 -7"/>
<path
android:fillColor="#777"
android:pathData="M333 156v7h-7a7 7 0 0 0 7 -7"/>
<path
android:fillColor="#777"
android:pathData="M0 156v7h7a7 7 0 0 1 -7 -7"/>
</vector>
图片有什么问题吗?圆的正方形中间的那条线?有一个底部的事实?我可以看到这张照片,但我不知道你不喜欢其中的哪一部分。名字下面的绿色方框。你能发布一个屏幕截图,当它看起来不错的时候,它看起来是什么样子吗?例如,一张填好的列表的截图。太棒了!非常感谢。