Android 如何使视图显示在下面
嘿,我想让一个视图显示在最后一个已创建的视图下方 它是这样表现的: 我想在上一个视图的下方显示下一个视图,因此对于添加的每个新视图,它都显示在下面。明白吗 这是我的密码。xml文件和java文件 listitem.xmlAndroid 如何使视图显示在下面,android,android-layout,android-listview,Android,Android Layout,Android Listview,嘿,我想让一个视图显示在最后一个已创建的视图下方 它是这样表现的: 我想在上一个视图的下方显示下一个视图,因此对于添加的每个新视图,它都显示在下面。明白吗 这是我的密码。xml文件和java文件 listitem.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layou
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="@+id/linearId"
android:padding="6dip"
>
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/icon"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip" android:layout_weight="1"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="@+id/txt2"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:src="@drawable/icon"
/>
<TextView
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="@+id/txt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearId"
android:orientation="vertical"
>
<TextView android:id="@+id/rowCount" android:layout_width="wrap_content"
android:layout_height="wrap_content" ></TextView>
<LinearLayout
android:id="@+id/linearId2"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
</LinearLayout>
</LinearLayout>
RatedCalls.java
public class RatedCalls extends Activity {
private static final String LOG_TAG = "RatedCalls";
private TableLayout table;
private CallDataHelper cdh;
private TableRow row;
private TableRow row2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listitem);
Log.i(LOG_TAG, "calling from onCreate()");
cdh = new CallDataHelper(this);
startService(new Intent(this, RatedCallsService.class));
Log.i(LOG_TAG, "Service called.");
Log.i(LOG_TAG, "before call fillList");
List<String> ratedCalls = new ArrayList<String>();
ratedCalls = this.cdh.selectTopCalls();
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId);
for (int i = 0; i < 1; i++) {
View item = inflater.inflate(R.layout.listitem, null);
TextView x = (TextView) item.findViewById(R.id.txt1);
x.setText(ratedCalls.get(0));
TextView ht = (TextView) item.findViewById(R.id.txt2);
ht.setText(ratedCalls.get(1));
ll.addView(item, ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
公共类RatedCalls扩展活动{
私有静态最终字符串日志\u TAG=“RatedCalls”;
专用表格;
私人呼叫数据助手cdh;
私人桌排;
私人桌子第2排;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.listitem);
i(Log_标记,“从onCreate()调用”);
cdh=新的CallDataHelper(此);
startService(新意图(这个,RatedCallsService.class));
Log.i(Log_标签,“调用服务”);
Log.i(Log_标签,“呼叫前填充列表”);
List ratedCalls=new ArrayList();
ratedCalls=this.cdh.selectTopCalls();
LayoutInflater充气器=(LayoutInflater)此
.getSystemService(上下文布局\充气机\服务);
LinearLayout ll=(LinearLayout)this.findViewById(R.id.linearId);
对于(int i=0;i<1;i++){
视图项=充气机。充气(R.layout.listitem,null);
TextView x=(TextView)item.findViewById(R.id.txt1);
x、 setText(ratedCalls.get(0));
TextView ht=(TextView)item.findViewById(R.id.txt2);
ht.setText(ratedCalls.get(1));
ll.addView(项目、视图组、布局参数、包装内容);
}
}
在xml文件中,将
上的android:orientation
属性设置为vertical
:
查看类文档和文档以了解更多信息。您的问题是应该从列表适配器使用listitem.xml。您要做的是,首先将内容视图设置为listitem.xml的实例,然后尝试将listitem的实例插入listitem的第一个LinearLayout。您应该使用listitem.xml作为您在自定义适配器的
getView()
方法中膨胀的视图。例如,请参阅中的QuoteAdapter
类,或者仅通过谷歌搜索“custom ArrayAdapter Android”来获取此主题的大量结果。嗯……我不完全理解需要什么,但可能需要添加Android:orientation=使用id=“linearId.”“vertical”到LinearLayout要工作,您需要为每个充气机创建两个不同的xml
在布局文件夹中,新建xmllayout/main2.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="@+id/linearId"
android:padding="6dip"
>
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/icon"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip" android:layout_weight="1"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="@+id/txt2"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:src="@drawable/icon"
/>
<TextView
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="@+id/txt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearId"
android:orientation="vertical"
>
<TextView android:id="@+id/rowCount" android:layout_width="wrap_content"
android:layout_height="wrap_content" ></TextView>
<LinearLayout
android:id="@+id/linearId2"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
</LinearLayout>
</LinearLayout>
然后您的布局/main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="@+id/linearId"
android:padding="6dip"
>
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/icon"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip" android:layout_weight="1"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="@+id/txt2"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:src="@drawable/icon"
/>
<TextView
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="@+id/txt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearId"
android:orientation="vertical"
>
<TextView android:id="@+id/rowCount" android:layout_width="wrap_content"
android:layout_height="wrap_content" ></TextView>
<LinearLayout
android:id="@+id/linearId2"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
</LinearLayout>
</LinearLayout>
然后你可以像这样设置你的活动
private int ELEMENTINEACHROW=2 ;
private int NOOFROW = 4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId);
for (int j = 0; j < NOOFROW; j++) {
View itemMain = inflater.inflate(R.layout.main, null, false);
TextView rowCount = (TextView) itemMain.findViewById(R.id.rowCount);
rowCount.setText("Row:"+(j+1));
LinearLayout ll2 = (LinearLayout) itemMain.findViewById(R.id.linearId2);
for (int i = 0; i < ELEMENTINEACHROW; i++) {
View item = inflater.inflate(R.layout.main2, null, false);
TextView x = (TextView) item.findViewById(R.id.txt1);
x.setText("test");
TextView ht = (TextView) item.findViewById(R.id.txt2);
ht.setText("good");
ll2.addView(item, ViewGroup.LayoutParams.FILL_PARENT);
}
ll.addView(itemMain, ViewGroup.LayoutParams.FILL_PARENT);
}
}
private int-ELEMENTINEACHROW=2;
私有int NOOFROW=4;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater充气器=(LayoutInflater)this.getSystemService(Context.LAYOUT\u充气器\u服务);
LinearLayout ll=(LinearLayout)this.findViewById(R.id.linearId);
对于(int j=0;j
希望它能给你一个想法。你的意思是在
LinearLayout
上,而不是ListView
,对吗?虽然这不是这个示例的实际问题,但这是我第一次想到的。listitem应该是一个单一的视图,插入到另一个垂直的LinearLayout中(实际上需要一个ListView)@KCopock yeah LinearLayout not ListView.现在修复你能给我一个更清晰的例子说明如何实现这个吗?我不太明白。我创建了ListAdatpter的一个对象,但当我尝试实例化它时,它说它不能疯狂。谢谢。这是我通常指给人们看的:Commonware关于ListView的书节选是excellent:好的,我会尝试第一个链接,然后我会在这里发布更新。谢谢。