Как сделать вид появится ниже - PullRequest
0 голосов
/ 13 января 2011

Эй, я хочу, чтобы вид отображался под последним созданным.

Показывается так:

alt text

Я хочу сделатьследующий вид отображается под последним, поэтому для каждого добавленного нового представления он отображается ниже.Понять?

Вот мой код.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);


    }
}

Ответы [ 4 ]

1 голос
/ 13 января 2011

Ваша проблема в том, что вы должны использовать listitem.xml из адаптера списка.То, что вы делаете, - сначала устанавливаете представление контента для экземпляра listitem.xml, затем пытаетесь вставить экземпляр listitem в первый LinearLayout listitem.Вы должны использовать listitem.xml как представление, которое вы надуваете в методе getView() вашего адаптера.См. Пример QuoteAdapter из этого вопроса или просто поищите в Google "custom ArrayAdapter Android" множество результатов по этой теме.

0 голосов
/ 14 января 2011

Чтобы работать, вам нужно создать два разных 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);
        }
    }

Надеюсь, это даст вам идею,

0 голосов
/ 13 января 2011

ммм ... Я не совсем понял, что требуется, но может быть вам нужно добавить android: ориентация = "вертикальный" в LinearLayout с id = "linearId".

0 голосов
/ 13 января 2011

В вашем XML-файле установите для атрибута android:orientation значение vertical на вашем <LinearLayout/>:

<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 и ориентация документация для получения дополнительной информации.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...