Java 在Android应用程序中,如何使ListView中的行适应其内容
好的,现在我有一个设置高度的列表视图 但是,当列表视图中某一行的内容增长时,该行不会增长 正如您在第二幅图像上看到的,第一行的内容被切断 当没有足够的空间时,有没有办法让单行展开 活动代码:Java 在Android应用程序中,如何使ListView中的行适应其内容,java,android,listview,android-listview,Java,Android,Listview,Android Listview,好的,现在我有一个设置高度的列表视图 但是,当列表视图中某一行的内容增长时,该行不会增长 正如您在第二幅图像上看到的,第一行的内容被切断 当没有足够的空间时,有没有办法让单行展开 活动代码: public class MainActivity extends ActionBarActivity { ArrayList<Location> arrayOfLocations; LocationAdapter adapter; @Override protected void on
public class MainActivity extends ActionBarActivity {
ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// Construct the data source
arrayOfLocations = new ArrayList<Location>();
// Create the adapter to convert the array to views
adapter = new LocationAdapter(this, arrayOfLocations);
adapter.add(new Location(R.drawable.ic_launcher, "Fruit Stand",
"We have the freshest fruit in the whole world!", "2 miles",
"8-5 mon-sat\nclosed sun"));
//getData();
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.listView1);
View header = (View) getLayoutInflater().inflate(
R.layout.listview_header, null);
listView.addHeaderView(header);
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
公共类MainActivity扩展了ActionBarActivity{
ArrayList ArrayFlocations;
位置适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.activity_main);
//构建数据源
arrayOfLocations=新的ArrayList();
//创建适配器以将阵列转换为视图
适配器=新位置适配器(此为ArrayFlocations);
适配器。添加(新位置(R.drawable.ic_启动器,“水果架”,
“我们有世界上最新鲜的水果!”,“2英里”,
“周一周六8-5点(周日关闭”);
//getData();
//将适配器连接到ListView
ListView ListView=(ListView)findViewById(R.id.listView1);
视图标题=(视图)GetLayoutFlater()。充气(
R.layout.listview_头,空);
addHeaderView(标题);
setAdapter(适配器);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
行的XML布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="140dip" >
<ImageView
android:id="@+id/imgIcon"
android:layout_width="90sp"
android:layout_height="120sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toRightOf="@id/imgIcon"
android:orientation="vertical"
android:paddingLeft="10sp" >
<TextView
android:id="@+id/tvName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp" />
<TextView
android:id="@+id/tvDetails"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp" />
<TextView
android:id="@+id/tvDistance"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp" />
<TextView
android:id="@+id/tvHours"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
非常感谢!也许您应该将行高更新为“换行内容”,而不是140行
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="140dip" >
...
</RelativeLayout>
...
可能是,您应该将行高更新为“包裹内容”,而不是140dip
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="140dip" >
...
</RelativeLayout>
...
您是否尝试将包裹内容
用于高度
而不是固定?您应该尝试将相对物的高度设置为包裹内容您是否尝试将包裹内容
用于高度
而不是固定?您应该尝试将相对物的高度设置为包裹内容是的,这很好解决了这是你的问题:)但是再看看答案,我设置了参数“minHeight”,所以行高不会小于140dip;)是的,很高兴它解决了你的问题:)但是再看看答案,我设置了参数“minHeight”,所以行高不会小于140dip;)