如何在 Android 中建立具有圓角的 listView?


此示例演示瞭如何在 android 中建立具有圓角的 listView。

步驟 1 - 在 Android Studio 中建立一個新專案,訪問 File ⇒ New Project 然後填寫所有必需的詳細資訊以建立一個新專案。

步驟 2 - 在 res/layout/activity_main.xml 中新增以下程式碼。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <ListView
      android:id="@+id/listView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_centerHorizontal="true">
   </ListView>
</RelativeLayout>

步驟 3 - 在 src/MainActivity.java 中新增以下程式碼

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ListView listView = findViewById(R.id.listView);
      String[] fruits = new String[]{"Mango","Apple","Grapes","PineApple","Banana","Kiwi", "Orange","Avocado","Strawberry"};
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, android.R.id.text1, fruits);
      listView.setAdapter(adapter);
      listView.setBackgroundResource(R.drawable.rounded_corners);
   }
}

步驟 4 - 在 res/drawable 中建立一個可繪製資原始檔並在 res/drawable/rounded_corners.xml 中新增以下程式碼;

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <gradient
      android:angle="90"
      android:endColor="#C0C0C0"
      android:startColor="#808080" />
   <corners
      android:bottomLeftRadius="16dp"
      android:bottomRightRadius="16dp"
      android:topLeftRadius="16dp"
      android:topRightRadius="16dp" />
   <stroke
      android:width="1px"
      android:color="#000000" />
</shape>

步驟 5 - 在 androidManifest.xml 中新增以下程式碼

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

讓我們嘗試執行應用程式。我假設你已經將真正的 Android 移動裝置與你的電腦相連。要從 Android Studio 執行應用程式,請開啟一個專案的活動檔案,然後單擊工具欄中的執行  圖示。選擇你的移動裝置,然後檢查在預設螢幕上顯示的移動裝置 -

單擊 此處 下載專案程式碼。

更新於:05-Aug-2019

736 次瀏覽

推動你的職業

透過完成此課程獲得認證

開始學習
廣告
© . All rights reserved.