【问题标题】:How to make a view appear below如何使视图出现在下面
【发布时间】:2011-01-13 19:43:57
【问题描述】:

嘿,我想让一个视图显示在最后一个已创建的视图下方。

它是这样显示的:

我想让下一个视图显示在最后一个视图下方,因此对于添加的每个新视图,它都会显示在下方。明白了吗?

这是我的代码。 xml文件和java文件。

listitem.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>

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);


    }
}

【问题讨论】:

    标签: android android-layout android-listview


    【解决方案1】:

    您的问题是您应该使用列表适配器中的 listitem.xml。您正在做的是,首先将您的内容视图设置为 listitem.xml 的实例,然后尝试将 listitem 的实例插入到 listitem 的第一个 LinearLayout 中。您应该使用 listitem.xml 作为您在自定义适配器的 getView() 方法中扩展的视图。有关示例,请参见 this question 中的 QuoteAdapter 类,或者仅在 Google 搜索“自定义 ArrayAdapter Android”以获取有关此主题的大量结果。

    【讨论】:

    • 你能给我一个更清楚的例子来说明如何实现这个吗?我不是很明白。我创建了一个 ListAdatpter 对象,但是当我尝试实例化它时,它说它不能被实例化。谢谢。
    • 这是我通常推荐给人们的一本:softwarepassion.com/…CommonsWare 关于 ListViews 的书摘也很棒:commonsware.com/Android/excerpt.pdf
    • 好的,我会尝试第一个链接,稍后我会在这里发布更新。谢谢。
    【解决方案2】:

    为了工作,你需要为每个充气机创建两个不同的 xml

    在你的布局文件夹中,新建 xml layout/main2.xml

    <?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>
    

    然后你的 layout/main.xml

    <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);
            }
        }
    

    希望它能给你这个想法。

    【讨论】:

      【解决方案3】:

      嗯...我不完全了解需要什么,但可能需要将 android:orientation="vertical" 添加到 id="linearId" 的 LinearLayout。

      【讨论】:

        【解决方案4】:

        在您的 xml 文件中,将您的 &lt;LinearLayout/&gt; 上的 android:orientation 属性设置为 vertical

        <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"
             android:orientation="vertical"/>
        

        查看LinearLayout 类文档和orientation 文档了解更多信息。

        【讨论】:

        • 你的意思是LinearLayout,不是ListView,对吧?不过,这实际上并不是这个示例的问题,尽管这是我最初认为的问题。 listitem 应该只是一个单独的视图,插入到另一个垂直的 LinearLayout 中(实际上需要一个 ListView)。
        • @kcoppock 是的,LinearLayout 不是 ListView。立即修复
        猜你喜欢
        • 2013-03-07
        • 2022-01-14
        • 1970-01-01
        • 2011-02-08
        • 2011-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多