Listview 中的内容是不可见的
Content in Listview is invisible
我是 Android 的新手,我目前正在开发一个包含带有简单文本的列表视图的应用程序。当用户单击列表视图中的文本时,将触发共享意图。我使用自定义适配器为列表视图中的文本设置了不同的字体。问题是当我 运行 正在查看列表视图但内容不可见的应用程序时。但是当我点击列表视图时,共享意图会被触发。我不知道是什么原因造成的。我在 Stack Overflow 和 google 中进行了搜索,并尝试了一些步骤,但没有成功。请帮帮我。提前致谢。
代码:
Main.xml
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.proda.technologies.app.testapp"
android:background="@drawable/inspirationalbg">
<ImageButton
android:layout_width="130dp"
android:layout_height="40dp"
android:id="@+id/back_Button"
android:background="@drawable/backbutton"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/list"
android:layout_below="@+id/back_Button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
list_item.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">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:id="@+id/instView"/>
</LinearLayout>
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context,R.layout.list_item,values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View rview = inflater.inflate(R.layout.list_item,null);
TextView txt = (TextView)rview.findViewById(R.id.instView);
AssetManager mgr = context.getAssets();
Typeface tf = Typeface.createFromAsset(mgr,"fonts/RalewayRegular.ttf");
txt.setTypeface(tf);
return rview;
}
}
Main.java
String[] phone = {"Nexus","Htc","Samsung","Oppo","Apple"};
Listview inslist;
inslist = (ListView)findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(Main.this,phone);
inslist.setAdapter(adapter);
我没有收到任何错误。我可以看到列表视图,其中的行分隔每个内容。但实际内容(文本)是不可见的。请帮助我
我没有在任何地方看到您在 TextView 中添加文本。
您能否将要在 TextView 视图中显示的数据放入 getView() 方法中,例如:
txt.setText(values.get(position));
您的 ListViewItem 包含一个 TextView,但它的值不是 init。
尝试在 xml 文件或 java 代码中为 textView 设置值。
尝试以下方法之一:
1.通过 XML 文件:list_item.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">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:id="@+id/instView"
android:text="This is text sample"/>
</LinearLayout>
2。通过 Java 代码:CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context,R.layout.list_item,values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View rview = inflater.inflate(R.layout.list_item,null);
TextView txt = (TextView)rview.findViewById(R.id.instView);
txt.setText(values[position]);
AssetManager mgr = context.getAssets();
Typeface tf = Typeface.createFromAsset(mgr,"fonts/RalewayRegular.ttf");
txt.setTypeface(tf);
return rview;
}
}
我是 Android 的新手,我目前正在开发一个包含带有简单文本的列表视图的应用程序。当用户单击列表视图中的文本时,将触发共享意图。我使用自定义适配器为列表视图中的文本设置了不同的字体。问题是当我 运行 正在查看列表视图但内容不可见的应用程序时。但是当我点击列表视图时,共享意图会被触发。我不知道是什么原因造成的。我在 Stack Overflow 和 google 中进行了搜索,并尝试了一些步骤,但没有成功。请帮帮我。提前致谢。
代码:
Main.xml
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.proda.technologies.app.testapp"
android:background="@drawable/inspirationalbg">
<ImageButton
android:layout_width="130dp"
android:layout_height="40dp"
android:id="@+id/back_Button"
android:background="@drawable/backbutton"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/list"
android:layout_below="@+id/back_Button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
list_item.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">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:id="@+id/instView"/>
</LinearLayout>
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context,R.layout.list_item,values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View rview = inflater.inflate(R.layout.list_item,null);
TextView txt = (TextView)rview.findViewById(R.id.instView);
AssetManager mgr = context.getAssets();
Typeface tf = Typeface.createFromAsset(mgr,"fonts/RalewayRegular.ttf");
txt.setTypeface(tf);
return rview;
}
}
Main.java
String[] phone = {"Nexus","Htc","Samsung","Oppo","Apple"};
Listview inslist;
inslist = (ListView)findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(Main.this,phone);
inslist.setAdapter(adapter);
我没有收到任何错误。我可以看到列表视图,其中的行分隔每个内容。但实际内容(文本)是不可见的。请帮助我
我没有在任何地方看到您在 TextView 中添加文本。
您能否将要在 TextView 视图中显示的数据放入 getView() 方法中,例如:
txt.setText(values.get(position));
您的 ListViewItem 包含一个 TextView,但它的值不是 init。 尝试在 xml 文件或 java 代码中为 textView 设置值。 尝试以下方法之一:
1.通过 XML 文件:list_item.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">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#ffffff"
android:id="@+id/instView"
android:text="This is text sample"/>
</LinearLayout>
2。通过 Java 代码:CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context,R.layout.list_item,values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View rview = inflater.inflate(R.layout.list_item,null);
TextView txt = (TextView)rview.findViewById(R.id.instView);
txt.setText(values[position]);
AssetManager mgr = context.getAssets();
Typeface tf = Typeface.createFromAsset(mgr,"fonts/RalewayRegular.ttf");
txt.setTypeface(tf);
return rview;
}
}