【问题标题】:Curved border for list view列表视图的弯曲边框
【发布时间】:2011-05-03 14:40:47
【问题描述】:

我是 android 和 java 的新手,我需要在 android 中创建一个简单的应用程序。我在此活动中有一个活动页面。这是我的活动页面

package com.tkcmu.dev;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class tkcmu extends Activity {
    private ListView lv1;

    @Override
        public void onCreate(Bundle icicle)
        {
        super.onCreate(icicle);
        setContentView(R.layout.main);


        ArrayList<String> listItems = new ArrayList<String>();

        try {
            URL recentUrl = new URL(
                    "To some link");
            URLConnection tc = recentUrl.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));

            String line;
            while ((line = in.readLine()) != null) {
                JSONArray ja = new JSONArray(line);

                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    listItems.add(jo.getString("content"));
                }
            }

    }catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        lv1=(ListView)findViewById(R.id.ListView01);
        // By using setAdpater method in listview we an add string array in list.
        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , listItems));
        }
}

而我的main.xml 是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/ListView01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10sp"
        android:layout_marginRight="10sp"
        android:layout_marginTop="10sp"
        android:layout_weight="1"
        android:background="@drawable/customshape"
        android:cacheColorHint="#FFFFFF"
        android:clickable="true"
        android:clipToPadding="true"
        android:dividerHeight="1px"
        android:drawSelectorOnTop="false"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:footerDividersEnabled="true"
        android:headerDividersEnabled="true"
        android:longClickable="true" />
</LinearLayout>

我还有一个 customshape.xml 可绘制的

 <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp" />
</shape>

我得到了整个自定义列表部分弯曲的结果,但我希望每一行都具有弯曲的背景。我已经在这里发布了我的整个代码(不包括AndroidManifest.xml),所以如果有人确切地告诉我我需要在此代码中进行哪些更改,我将不胜感激。任何机构都知道如何实施它。

【问题讨论】:

  • 不确定你是否还需要它,但我已经为此创建了一个 android 库。看看这个UITableView for Android
  • 您正在设置整个列表视图的背景。如果您为该列表视图使用自定义布局,则将您的自定义形状背景设置为列表项的根布局,而不是列表视图

标签: android


【解决方案1】:

添加

 android:background="@drawable/customshape" 

到 listview row.xml 文件的根视图,而不是将其应用到 listview

【讨论】:

    【解决方案2】:

    我从来没有为圆角做过这个,但我会尝试为列表提供一个自定义适配器。自定义适配器将允许您指定要为每个项目使用的视图。在您的情况下,您可以尝试提供具有圆角边框的视图(使用形状 XML 定义)。

    为此,您将替换该行 lv1.setAdapter(new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1 , listItems));

    使用实现getView() 方法的BaseAdapter。

    实际上有数百个关于如何做到这一点的教程,但this one 似乎更详细。

    【讨论】:

      【解决方案3】:

      我建议您使用自定义适配器,您可以在其中将形状设置为背景。这在那儿很容易实现。不过既然是新手,那就有点难了。所以有些东西我不推荐,但可能是一个解决方案:在 android.R.layout.simple_list_item_1 上按 control + B。这是由 Android 团队编写的原始列表视图 XML。所以你必须覆盖它并在这里设置你的背景形状(你写的形状,将它设置为背景)。

      【讨论】:

        【解决方案4】:

        在适配器类中使用此布局并膨胀此布局。在您的项目中导入最新的 cardview 库。

         <?xml version="1.0" encoding="utf-8"?>
         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardCornerRadius="8dp"
            card_view:cardUseCompatPadding="true">
        
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
        
                <RelativeLayout
                    android:id="@+id/relative_card_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
        
                    <ImageView
                        android:id="@+id/country_photo"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:contentDescription="@string/action_settings"
                        android:scaleType="centerCrop"
                        android:src="@drawable/sweden" />
        
                    <TextView
                        android:id="@+id/title_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:gravity="center"
                        android:text="@string/app_name"
                        android:textColor="@color/colorAccent"
                        android:textSize="24sp"
                        android:textStyle="bold" />
        
                </RelativeLayout>
        
                <TextView
                    android:id="@+id/title_description"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginStart="8dp"
                    android:text="@string/app_name"
                    android:layout_below="@+id/relative_card_layout"
                    android:textColor="@color/colorAccent"
                    android:textSize="18sp"
                    android:padding="8dp"
                    android:textStyle="bold" />
            </RelativeLayout>
        
        </android.support.v7.widget.CardView>
        </LinearLayout>
        

        【讨论】:

          【解决方案5】:

          最简单的方法是定义这样的 xml 并将其命名为 my_list_item 或任何您喜欢的名称:

          <?xml version="1.0" encoding="utf-8"?>
          <TextView xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="wrap_content"
             android:background="@drawable/customshape"
             android:layout_height="wrap_content">
          </TextView>
          

          只需在您的代码中更改这一行:

          lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.my_list_item , listItems));
          

          重要提示:

          为customshape 可绘制对象的背景设置颜色。不定义任何颜色会导致一些旧的安卓平板电脑(Axtrom,Asus)出现黑色背景。像这样的:

          <?xml version="1.0" encoding="utf-8"?>
          <shape xmlns:android="http://schemas.android.com/apk/res/android">
             <corners android:radius="10dp"/>
             <solid android:color="#ffffff"/>
          </shape>
          

          要提到的另一件事是,您可以更改您创建的 xml 中行的所有内容。例如 textcolor、textsize、gravity 等 ....

          【讨论】:

            【解决方案6】:

            创建可绘制布局

            <?xml version="1.0" encoding="utf-8"?>
            <shape xmlns:android="http://schemas.android.com/apk/res/android">
               <corners android:radius="5dp"/>
               <solid android:color="#ff0000"/>
            </shape>
            

            然后在listView的后台使用它

             android:background="@drawable/yourLayoutName" 
            

            【讨论】:

              【解决方案7】:

              一个简单的方法是使用 CardView。这是添加依赖项所需的依赖项:com.android.support:cardview-v7:27.0.2。将此添加到您的build.gradle

              接下来,只需浏览到simple_list_item_1.xml,即您已在其中创建单行布局的 XML 文件,该布局将使用适配器显示在 ListView 中!

              在此 XML 文件中,将 CardView 设为您的父布局,例如,如果这是 simple_list_item_1.xml 中的先前代码:

              <RelativeLayout
                  android:id="main_layout"
                  android:width="match_parent"
                  android:height="wrap_content">
              
                  ...
                  //Other layouts and normal code here.
                  ...
              
              </RelativeLayout>
              

              然后,您应该将代码更改为:

              <android.support.v7.widget.CardView
                  xmlns:card_view="http://schemas.android.com/apk/res-auto"
                  android:id="@+id/card_view"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  card_view:cardCornerRadius="4dp">
              
                  <RelativeLayout
                      android:id="main_layout"
                      android:width="match_parent"
                      android:height="wrap_content">
              
                      ...
                      //Other layouts and normal code here.
                      ...
              
                  </RelativeLayout>
              
              </android.support.v7.widget.CardView>
              

              在这里,您可以使用 CardView 的card_view:cardCornerRadius 属性为整个布局赋予圆边!

              附:作为额外的奖励,您还可以使用card_view:cardElevation 属性为您的布局提供凸起的外观,如果您使用此属性,请确保在各个方向上留出一些空间以提供浅色阴影效果!

              希望我能帮上忙;)

              这里有一些资源可以帮助您:

              1) official documentation.

              2) 一个basic tutorial (with a RecyclerView)

              3) 我这边的免责声明:传统上,CardView 只与RecyclerView's 一起使用,这不得不导致人们普遍认为CardView 只能与RecyclerView 一起使用,什么都没有别的。这是一个常见的误解,CardView 是一个设计元素,不会以任何方式绑定到RecyclerView,它们可以以您想要的任何方式使用!

              这就是圆角 CardView 的样子(减去 ListView 即 CardView 本身) - 请参阅 one of my designs.

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2014-09-21
                • 1970-01-01
                • 2018-10-28
                • 2010-09-26
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多