Images/Vector 资产现在显示在 ListView CustomAdapter 中
Images/Vector Assets now showing in ListView CustomAdapter
因此,我正在尝试使用图像和文本制作自定义列表视图,因此我首先创建了一个布局:
custom_list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content">
<ImageView
android:id="@+id/imageViewProduct"
android:layout_width="100dp"
android:layout_height="120dp"
app:srcCompat="@drawable/ic_teamwork" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/bookName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-black"
android:padding="5dp"
android:text="Book Name" />
<TextView
android:padding="5dp"
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Author"/>
<RelativeLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
app:srcCompat="@drawable/ic_coin"
android:layout_toStartOf="@+id/price" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:fontFamily="sans-serif-black"
android:gravity="right"
android:text="1000" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
然后我有一个 Activity,我正在使用另一个 activity.
的 Intents 打开它
ListingActivity.java
package com.rishav.vitbooks;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import java.util.ArrayList;
import es.dmoral.toasty.Toasty;
public class ListingActivity extends AppCompatActivity {
private static final String TAG = "my_logs";
ArrayList<Book> arrayList;
ListView lv;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listing);
arrayList = new ArrayList<>();
lv = findViewById(R.id.listView);
Log.d(TAG, "onCreate: init");
Intent intent = getIntent();
String email = intent.getStringExtra("User");
Toasty.info(ListingActivity.this, "Welcome back " + email.trim(), Toasty.LENGTH_SHORT, true).show();
arrayList.add(new Book("Fundamentals Of Physics", "H.C Verma",200));
arrayList.add(new Book("Android Dev", "Sushrut dai",1000));
arrayList.add(new Book("Name here", "Risav",1));
CustomListAdapter adapter = new CustomListAdapter(
getApplicationContext(), R.layout.custom_list_layout, arrayList
);
lv.setAdapter(adapter);
}
}
activity_listening.xml
<?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">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"/>
</LinearLayout>
但是我在列表视图中看不到矢量资源,尽管我确实按照我的意愿在那里获取了文本。我的 drawable 目录中有 ic_teamwork
和 ic_coin
。事实上,如果我将内容视图从 activity_listening
更改为“custom_list_layout”,我可以看到图像,但当它们被放大到列表视图中时看不到。
这是我的 CustomAdapter:
public class CustomListAdapter extends ArrayAdapter<Book> {
private ArrayList<Book> books;
private Context context;
private int resource;
public CustomListAdapter (Context context, int resource, ArrayList<Book> objects) {
super(context, resource, objects);
this.books = objects;
this.context = context;
this.resource = resource;
}
@NonNull
@Override
public View getView (int position, View convertView, @NonNull ViewGroup parent) {
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) getContext()
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_list_layout, null, true);
}
Book book = getItem(position);
TextView txtName = convertView.findViewById(R.id.bookName);
assert book != null;
txtName.setText(book.getName());
TextView txtAuthor = convertView.findViewById(R.id.author);
txtAuthor.setText(book.getAuthor());
TextView txtPrice = convertView.findViewById(R.id.price);
txtPrice.setText(String.valueOf(book.getSellPrice()));
return convertView;
}
}
如果这很重要,我正在尝试处理 this 示例。
这是我的 gradle 个文件:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0-alpha13'
classpath 'com.google.gms:google-services:4.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
和
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.rishav.vitbooks"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
vectorDrawables {
useSupportLibrary = true
}
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'android.arch.navigation:navigation-fragment:1.0.0-rc02'
implementation 'android.arch.navigation:navigation-ui:1.0.0-rc02'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-auth:16.0.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.github.GrenderG:Toasty:1.4.0'
}
apply plugin: 'com.google.gms.google-services'
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
xmlns:tools="http://schemas.android.com/tools"
package="com.rishav.vitbooks">
<dist:module dist:instant="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<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/DarkTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".ListingActivity"
android:theme="@style/AppTheme">
</activity>
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
<activity android:name=".HelpActivity"></activity>
</application>
</manifest>
为什么我在列表视图中看不到 images/vector 资产,但在 custom_list_layout 中可以看到它们?我该如何解决这个问题?
移除后:
vectorDrawables.useSupportLibrary = true
从您的 build.gradle
(您有 2 次!)文件中,您可以在 xml
布局中使用 android:src
标签。
我创建了新的矢量文件(在 res > drawable > ic_directions_boat.xml
中):
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#002F6C"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M20,21c-1.39,0 -2.78,-0.47 -4,-1.32 -2.44,1.71 -5.56,1.71 -8,0C6.78,20.53 5.39,21 4,21H2v2h2c1.38,0 2.74,-0.35 4,-0.99 2.52,1.29 5.48,1.29 8,0 1.26,0.65 2.62,0.99 4,0.99h2v-2h-2zM3.95,19H4c1.6,0 3.02,-0.88 4,-2 0.98,1.12 2.4,2 4,2s3.02,-0.88 4,-2c0.98,1.12 2.4,2 4,2h0.05l1.89,-6.68c0.08,-0.26 0.06,-0.54 -0.06,-0.78s-0.34,-0.42 -0.6,-0.5L20,10.62V6c0,-1.1 -0.9,-2 -2,-2h-3V1H9v3H6c-1.1,0 -2,0.9 -2,2v4.62l-1.29,0.42c-0.26,0.08 -0.48,0.26 -0.6,0.5s-0.15,0.52 -0.06,0.78L3.95,19zM6,6h12v3.97L12,8 6,9.97V6z" />
</vector>
并更改了您的 (srcCompat
):
<ImageView
android:id="@+id/imageViewProduct"
android:layout_width="100dp"
android:layout_height="120dp"
app:srcCompat="@drawable/ic_teamwork" />
至(将我的 xml
用作 src
):
<ImageView
android:id="@+id/imageViewProduct"
android:layout_width="100dp"
android:layout_height="120dp"
android:src="@drawable/ic_directions_boat" />
我有这样的效果:
因此,我正在尝试使用图像和文本制作自定义列表视图,因此我首先创建了一个布局:
custom_list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content">
<ImageView
android:id="@+id/imageViewProduct"
android:layout_width="100dp"
android:layout_height="120dp"
app:srcCompat="@drawable/ic_teamwork" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/bookName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-black"
android:padding="5dp"
android:text="Book Name" />
<TextView
android:padding="5dp"
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Author"/>
<RelativeLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
app:srcCompat="@drawable/ic_coin"
android:layout_toStartOf="@+id/price" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:fontFamily="sans-serif-black"
android:gravity="right"
android:text="1000" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
然后我有一个 Activity,我正在使用另一个 activity.
的 Intents 打开它ListingActivity.java
package com.rishav.vitbooks;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import java.util.ArrayList;
import es.dmoral.toasty.Toasty;
public class ListingActivity extends AppCompatActivity {
private static final String TAG = "my_logs";
ArrayList<Book> arrayList;
ListView lv;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listing);
arrayList = new ArrayList<>();
lv = findViewById(R.id.listView);
Log.d(TAG, "onCreate: init");
Intent intent = getIntent();
String email = intent.getStringExtra("User");
Toasty.info(ListingActivity.this, "Welcome back " + email.trim(), Toasty.LENGTH_SHORT, true).show();
arrayList.add(new Book("Fundamentals Of Physics", "H.C Verma",200));
arrayList.add(new Book("Android Dev", "Sushrut dai",1000));
arrayList.add(new Book("Name here", "Risav",1));
CustomListAdapter adapter = new CustomListAdapter(
getApplicationContext(), R.layout.custom_list_layout, arrayList
);
lv.setAdapter(adapter);
}
}
activity_listening.xml
<?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">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"/>
</LinearLayout>
但是我在列表视图中看不到矢量资源,尽管我确实按照我的意愿在那里获取了文本。我的 drawable 目录中有 ic_teamwork
和 ic_coin
。事实上,如果我将内容视图从 activity_listening
更改为“custom_list_layout”,我可以看到图像,但当它们被放大到列表视图中时看不到。
这是我的 CustomAdapter:
public class CustomListAdapter extends ArrayAdapter<Book> {
private ArrayList<Book> books;
private Context context;
private int resource;
public CustomListAdapter (Context context, int resource, ArrayList<Book> objects) {
super(context, resource, objects);
this.books = objects;
this.context = context;
this.resource = resource;
}
@NonNull
@Override
public View getView (int position, View convertView, @NonNull ViewGroup parent) {
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) getContext()
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_list_layout, null, true);
}
Book book = getItem(position);
TextView txtName = convertView.findViewById(R.id.bookName);
assert book != null;
txtName.setText(book.getName());
TextView txtAuthor = convertView.findViewById(R.id.author);
txtAuthor.setText(book.getAuthor());
TextView txtPrice = convertView.findViewById(R.id.price);
txtPrice.setText(String.valueOf(book.getSellPrice()));
return convertView;
}
}
如果这很重要,我正在尝试处理 this 示例。
这是我的 gradle 个文件:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0-alpha13'
classpath 'com.google.gms:google-services:4.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
和
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.rishav.vitbooks"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
vectorDrawables {
useSupportLibrary = true
}
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'android.arch.navigation:navigation-fragment:1.0.0-rc02'
implementation 'android.arch.navigation:navigation-ui:1.0.0-rc02'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-auth:16.0.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.github.GrenderG:Toasty:1.4.0'
}
apply plugin: 'com.google.gms.google-services'
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
xmlns:tools="http://schemas.android.com/tools"
package="com.rishav.vitbooks">
<dist:module dist:instant="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<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/DarkTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".ListingActivity"
android:theme="@style/AppTheme">
</activity>
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
<activity android:name=".HelpActivity"></activity>
</application>
</manifest>
为什么我在列表视图中看不到 images/vector 资产,但在 custom_list_layout 中可以看到它们?我该如何解决这个问题?
移除后:
vectorDrawables.useSupportLibrary = true
从您的 build.gradle
(您有 2 次!)文件中,您可以在 xml
布局中使用 android:src
标签。
我创建了新的矢量文件(在 res > drawable > ic_directions_boat.xml
中):
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#002F6C"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M20,21c-1.39,0 -2.78,-0.47 -4,-1.32 -2.44,1.71 -5.56,1.71 -8,0C6.78,20.53 5.39,21 4,21H2v2h2c1.38,0 2.74,-0.35 4,-0.99 2.52,1.29 5.48,1.29 8,0 1.26,0.65 2.62,0.99 4,0.99h2v-2h-2zM3.95,19H4c1.6,0 3.02,-0.88 4,-2 0.98,1.12 2.4,2 4,2s3.02,-0.88 4,-2c0.98,1.12 2.4,2 4,2h0.05l1.89,-6.68c0.08,-0.26 0.06,-0.54 -0.06,-0.78s-0.34,-0.42 -0.6,-0.5L20,10.62V6c0,-1.1 -0.9,-2 -2,-2h-3V1H9v3H6c-1.1,0 -2,0.9 -2,2v4.62l-1.29,0.42c-0.26,0.08 -0.48,0.26 -0.6,0.5s-0.15,0.52 -0.06,0.78L3.95,19zM6,6h12v3.97L12,8 6,9.97V6z" />
</vector>
并更改了您的 (srcCompat
):
<ImageView
android:id="@+id/imageViewProduct"
android:layout_width="100dp"
android:layout_height="120dp"
app:srcCompat="@drawable/ic_teamwork" />
至(将我的 xml
用作 src
):
<ImageView
android:id="@+id/imageViewProduct"
android:layout_width="100dp"
android:layout_height="120dp"
android:src="@drawable/ic_directions_boat" />
我有这样的效果: