android如何自定义ListView的高度?
How to customized the ListView height in android?
我是 android 的初学者。我想显示一个包含数字的自定义列表视图。我定制了我的 ListView
尺寸(android:layout_height="50dp"
)。但是当我 运行 应用程序时,它显示的大小很大,但没有给出。我不知道如何解决这个问题。请任何人帮助我。
activity_topic_list.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.yuvi.secrectsforhappylife.TopicList">
<ListView
android:id="@+id/topiclist"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
titledesign.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/storylist">
<TextView
android:id="@+id/topictitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/title"
android:textSize="12pt"
android:textStyle="bold"
android:textColor="@android:color/background_light" />
</RelativeLayout>
TopicList.java
public class TopicList extends AppCompatActivity {
String topic[]={"one","two","three","four","five","six","seven","eight","nine","ten"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topic_list);
ListView listView=(ListView)findViewById(R.id.topiclist);
CustomAdapter customAdapter=new CustomAdapter();
listView.setAdapter(customAdapter);
}
class CustomAdapter extends BaseAdapter
{
@Override
public int getCount() {
return topic.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=getLayoutInflater().inflate(R.layout.titledesign,null);
TextView title=(TextView)convertView.findViewById(R.id.topictitle);
title.setText(topic[position]);
return convertView;
}
}
}
在 titledesign.xml
上尝试对父布局进行以下更改
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/storylist">
如果您希望您的列表捕获设备屏幕的整个宽度,请在 layout_width 上进行更改,例如
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/storylist">
这应该能帮到你..
ListAdapter listadp = lv_ancillaryservice.getAdapter();
if (listadp != null) {
int totalHeight = 0;
for (int i = 0; i < listadp.getCount(); i++) {
View listItem = listadp.getView(i, null, lv_ancillaryservice);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = lv_ancillaryservice.getLayoutParams();
params.height = totalHeight + (lv_ancillaryservice.getDividerHeight() * (listadp.getCount() - 1));
lv_ancillaryservice.setLayoutParams(params);
lv_ancillaryservice.requestLayout();
I want to fix the list cell height
仅供参考
您想为每个 Cell
添加 HEIGHT
然后
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/storylist">
<TextView
android:id="@+id/topictitle"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/title"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="@android:color/background_light" />
</RelativeLayout>
备注
使用 TextView 大小单位 sp 而不是 pt 。
将列表视图创建为
activity_word.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="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:padding="5dp"
android:textSize="25dp"
android:gravity="center_horizontal|center_vertical"
android:text="Text"
android:id="@+id/text_list_view" />
</LinearLayout>
创建另一个 xml 文件并在其中创建一个文本视图
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/listView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
在 Toplist java class 中将其添加为 setContentView
setContentView(R.layout.activity_word);
我是 android 的初学者。我想显示一个包含数字的自定义列表视图。我定制了我的 ListView
尺寸(android:layout_height="50dp"
)。但是当我 运行 应用程序时,它显示的大小很大,但没有给出。我不知道如何解决这个问题。请任何人帮助我。
activity_topic_list.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.yuvi.secrectsforhappylife.TopicList">
<ListView
android:id="@+id/topiclist"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
titledesign.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/storylist">
<TextView
android:id="@+id/topictitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/title"
android:textSize="12pt"
android:textStyle="bold"
android:textColor="@android:color/background_light" />
</RelativeLayout>
TopicList.java
public class TopicList extends AppCompatActivity {
String topic[]={"one","two","three","four","five","six","seven","eight","nine","ten"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topic_list);
ListView listView=(ListView)findViewById(R.id.topiclist);
CustomAdapter customAdapter=new CustomAdapter();
listView.setAdapter(customAdapter);
}
class CustomAdapter extends BaseAdapter
{
@Override
public int getCount() {
return topic.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=getLayoutInflater().inflate(R.layout.titledesign,null);
TextView title=(TextView)convertView.findViewById(R.id.topictitle);
title.setText(topic[position]);
return convertView;
}
}
}
在 titledesign.xml
上尝试对父布局进行以下更改<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/storylist">
如果您希望您的列表捕获设备屏幕的整个宽度,请在 layout_width 上进行更改,例如
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/storylist">
这应该能帮到你..
ListAdapter listadp = lv_ancillaryservice.getAdapter();
if (listadp != null) {
int totalHeight = 0;
for (int i = 0; i < listadp.getCount(); i++) {
View listItem = listadp.getView(i, null, lv_ancillaryservice);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = lv_ancillaryservice.getLayoutParams();
params.height = totalHeight + (lv_ancillaryservice.getDividerHeight() * (listadp.getCount() - 1));
lv_ancillaryservice.setLayoutParams(params);
lv_ancillaryservice.requestLayout();
I want to fix the list cell height
仅供参考
您想为每个 Cell
添加 HEIGHT然后
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/storylist">
<TextView
android:id="@+id/topictitle"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/title"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="@android:color/background_light" />
</RelativeLayout>
备注
使用 TextView 大小单位 sp 而不是 pt 。
将列表视图创建为 activity_word.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="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:padding="5dp"
android:textSize="25dp"
android:gravity="center_horizontal|center_vertical"
android:text="Text"
android:id="@+id/text_list_view" />
</LinearLayout>
创建另一个 xml 文件并在其中创建一个文本视图 listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/listView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
在 Toplist java class 中将其添加为 setContentView setContentView(R.layout.activity_word);