在Android中加载动态布局

时间:2014-05-02 05:17:41

标签: android android-layout layout dynamic

我目前正在制作一个使用动态布局的应用程序。这是我第一次这样做,而我只是为了学习它而做。我设法让它加载大部分数据并正确显示,但我遇到第一项问题。由于某种原因,它会跳过它并从第2项开始。它将其加载到变量中但由于某种原因未显示。我相信它与布局参数有关,但我找不到它的行为方式。

这是它的外观(请注意,它应该以141- ...项目开始。它被加载到变量tho但未显示)

enter image description here

这是我用来根据变量

加载项目的代码
for (int i = 0; i <= id_l; i++)
            {
                RelativeLayout container = (RelativeLayout)findViewById(R.id.popisLinija);
                View item_layout = getLayoutInflater().inflate(R.layout.item_layout, null);
                Integer l = id_l;
                Log.d("id length: ", l.toString());
                Log.d("ID: ", id[i].toString());
                Log.d("Linija br: ", broj_linije[i]);
                Integer i2 = i;
                Log.d("i= ", i2.toString());

                item_layout.setId(i);
                item_layout.setTag("View");

                final Integer h = item_layout.getId();

                // fill in any details dynamically here
                assert item_layout != null;
                TextView textView = (TextView) item_layout.findViewById(R.id.item_text);

                if(broj_linije[i].equals("000"))
                {
                    textView.setText(naziv_linije[i]);
                }
                else
                {
                    textView.setText(broj_linije[i] + " - " + naziv_linije[i]);
                }


                textView.setTag("Text");


                RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                if(i == 0)
                {
                    if (rlp != null) {
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, container.getId());
                    }
                }
                if(i > 0)
                {
                    if (rlp != null) {
                        rlp.addRule(RelativeLayout.BELOW, i-1);
                    }
                }


                FrameLayout item_button = (FrameLayout) item_layout.findViewById(R.id.item_button);
                item_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0)
                    {
                        Integer a =  h;
                        Toast.makeText(getApplicationContext(), "Clicked me", Toast.LENGTH_SHORT).show();
                        Log.d("clicked", h.toString());
                    }
                });

                item_layout.setLayoutParams(rlp);
                container.addView(item_layout);
            }

我已检查项目是否已加载

> 05-02 07:09:00.116  21204-21204/com.example.app D/ID:﹕ 0 05-02 <<< ITEM 141 VARIABLE
> 07:09:00.116  21204-21204/com.example.app D/Linija br:﹕ 141 05-02 <<< ITEM 141 VARIABLE
> 07:09:00.116  21204-21204/com.example.app D/i=﹕ 0 05-02 07:09:00.166  <<< ITEM 141 VARIABLE
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.166 
> 21204-21204/com.example.app D/ID:﹕ 1 05-02 07:09:00.166 
> 21204-21204/com.example.app D/Linija br:﹕ 142 05-02 07:09:00.166 
> 21204-21204/com.example.app D/i=﹕ 1 05-02 07:09:00.176 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.176 
> 21204-21204/com.example.app D/ID:﹕ 2 05-02 07:09:00.176 
> 21204-21204/com.example.app D/Linija br:﹕ 143 05-02 07:09:00.176 
> 21204-21204/com.example.app D/i=﹕ 2 05-02 07:09:00.206 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.206 
> 21204-21204/com.example.app D/ID:﹕ 3 05-02 07:09:00.206 
> 21204-21204/com.example.app D/Linija br:﹕ 144 05-02 07:09:00.206 
> 21204-21204/com.example.app D/i=﹕ 3 05-02 07:09:00.216 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.216 
> 21204-21204/com.example.app D/ID:﹕ 4 05-02 07:09:00.216 
> 21204-21204/com.example.app D/Linija br:﹕ 145 05-02 07:09:00.216 
> 21204-21204/com.example.app D/i=﹕ 4 05-02 07:09:00.226 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.226 
> 21204-21204/com.example.app D/ID:﹕ 5 05-02 07:09:00.226 
> 21204-21204/com.example.app D/Linija br:﹕ 146 05-02 07:09:00.226 
> 21204-21204/com.example.app D/i=﹕ 5 05-02 07:09:00.246 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.246 
> 21204-21204/com.example.app D/ID:﹕ 6 05-02 07:09:00.246 
> 21204-21204/com.example.app D/Linija br:﹕ 147 05-02 07:09:00.246 
> 21204-21204/com.example.app D/i=﹕ 6 05-02 07:09:00.276 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.276 
> 21204-21204/com.example.app D/ID:﹕ 7 05-02 07:09:00.276 
> 21204-21204/com.example.app D/Linija br:﹕ 148 05-02 07:09:00.276 
> 21204-21204/com.example.app D/i=﹕ 7 05-02 07:09:00.316 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.316 
> 21204-21204/com.example.app D/ID:﹕ 8 05-02 07:09:00.316 
> 21204-21204/com.example.app D/Linija br:﹕ 149 05-02 07:09:00.316 
> 21204-21204/com.example.app D/i=﹕ 8 05-02 07:09:00.316 
> 21204-21208/com.example.app D/dalvikvm﹕ GC_CONCURRENT freed 949K, 37%
> free 7958K/12524K, paused 9ms+17ms, total 92ms 05-02 07:09:00.326 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.326 
> 21204-21204/com.example.app D/ID:﹕ 9 05-02 07:09:00.326 
> 21204-21204/com.example.app D/Linija br:﹕ 150 05-02 07:09:00.326 
> 21204-21204/com.example.app D/i=﹕ 9 05-02 07:09:00.336 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.336 
> 21204-21204/com.example.app D/ID:﹕ 10 05-02 07:09:00.336 
> 21204-21204/com.example.app D/Linija br:﹕ 150A 05-02 07:09:00.336 
> 21204-21204/com.example.app D/i=﹕ 10 05-02 07:09:00.346 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.346 
> 21204-21204/com.example.app D/ID:﹕ 11 05-02 07:09:00.346 
> 21204-21204/com.example.app D/Linija br:﹕ 151 05-02 07:09:00.346 
> 21204-21204/com.example.app D/i=﹕ 11 05-02 07:09:00.366 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.366 
> 21204-21204/com.example.app D/ID:﹕ 12 05-02 07:09:00.366 
> 21204-21204/com.example.app D/Linija br:﹕ 153 05-02 07:09:00.366 
> 21204-21204/com.example.app D/i=﹕ 12 05-02 07:09:00.376 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.376 
> 21204-21204/com.example.app D/ID:﹕ 13 05-02 07:09:00.376 
> 21204-21204/com.example.app D/Linija br:﹕ 155 05-02 07:09:00.376 
> 21204-21204/com.example.app D/i=﹕ 13 05-02 07:09:00.386 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.386 
> 21204-21204/com.example.app D/ID:﹕ 14 05-02 07:09:00.386 
> 21204-21204/com.example.app D/Linija br:﹕ 156 05-02 07:09:00.386 
> 21204-21204/com.example.app D/i=﹕ 14 05-02 07:09:00.396 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.396 
> 21204-21204/com.example.app D/ID:﹕ 15 05-02 07:09:00.396 
> 21204-21204/com.example.app D/Linija br:﹕ 157 05-02 07:09:00.396 
> 21204-21204/com.example.app D/i=﹕ 15 05-02 07:09:00.406 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.406 
> 21204-21204/com.example.app D/ID:﹕ 16 05-02 07:09:00.406 
> 21204-21204/com.example.app D/Linija br:﹕ 158 05-02 07:09:00.406 
> 21204-21204/com.example.app D/i=﹕ 16 05-02 07:09:00.416 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.416 
> 21204-21204/com.example.app D/ID:﹕ 17 05-02 07:09:00.416 
> 21204-21204/com.example.app D/Linija br:﹕ 159 05-02 07:09:00.416 
> 21204-21204/com.example.app D/i=﹕ 17 05-02 07:09:00.426 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.426 
> 21204-21204/com.example.app D/ID:﹕ 18 05-02 07:09:00.436 
> 21204-21204/com.example.app D/Linija br:﹕ 160 05-02 07:09:00.436 
> 21204-21204/com.example.app D/i=﹕ 18 05-02 07:09:00.436 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.436 
> 21204-21204/com.example.app D/ID:﹕ 19 05-02 07:09:00.436 
> 21204-21204/com.example.app D/Linija br:﹕ 162 05-02 07:09:00.436 
> 21204-21204/com.example.app D/i=﹕ 19 05-02 07:09:00.446 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.446 
> 21204-21204/com.example.app D/ID:﹕ 20 05-02 07:09:00.446 
> 21204-21204/com.example.app D/Linija br:﹕ 260 05-02 07:09:00.446 
> 21204-21204/com.example.app D/i=﹕ 20 05-02 07:09:00.456 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.456 
> 21204-21204/com.example.app D/ID:﹕ 21 05-02 07:09:00.456 
> 21204-21204/com.example.app D/Linija br:﹕ 260A 05-02 07:09:00.456 
> 21204-21204/com.example.app D/i=﹕ 21 05-02 07:09:00.476 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.476 
> 21204-21204/com.example.app D/ID:﹕ 22 05-02 07:09:00.476 
> 21204-21204/com.example.app D/Linija br:﹕ 261 05-02 07:09:00.476 
> 21204-21204/com.example.app D/i=﹕ 22 05-02 07:09:00.486 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.486 
> 21204-21204/com.example.app D/ID:﹕ 23 05-02 07:09:00.486 
> 21204-21204/com.example.app D/Linija br:﹕ 262 05-02 07:09:00.486 
> 21204-21204/com.example.app D/i=﹕ 23 05-02 07:09:00.496 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.496 
> 21204-21204/com.example.app D/ID:﹕ 24 05-02 07:09:00.496 
> 21204-21204/com.example.app D/Linija br:﹕ 263 05-02 07:09:00.496 
> 21204-21204/com.example.app D/i=﹕ 24 05-02 07:09:00.506 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.506 
> 21204-21204/com.example.app D/ID:﹕ 25 05-02 07:09:00.506 
> 21204-21204/com.example.app D/Linija br:﹕ 264 05-02 07:09:00.506 
> 21204-21204/com.example.app D/i=﹕ 25 05-02 07:09:00.516 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.516 
> 21204-21204/com.example.app D/ID:﹕ 26 05-02 07:09:00.516 
> 21204-21204/com.example.app D/Linija br:﹕ 265 05-02 07:09:00.516 
> 21204-21204/com.example.app D/i=﹕ 26 05-02 07:09:00.526 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.526 
> 21204-21204/com.example.app D/ID:﹕ 27 05-02 07:09:00.526 
> 21204-21204/com.example.app D/Linija br:﹕ 266 05-02 07:09:00.526 
> 21204-21204/com.example.app D/i=﹕ 27 05-02 07:09:00.536 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.536 
> 21204-21204/com.example.app D/ID:﹕ 28 05-02 07:09:00.536 
> 21204-21204/com.example.app D/Linija br:﹕ 267 05-02 07:09:00.536 
> 21204-21204/com.example.app D/i=﹕ 28 05-02 07:09:00.546 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.546 
> 21204-21204/com.example.app D/ID:﹕ 29 05-02 07:09:00.546 
> 21204-21204/com.example.app D/Linija br:﹕ 000 05-02 07:09:00.546 
> 21204-21204/com.example.app D/i=﹕ 29 05-02 07:09:00.556 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.556 
> 21204-21204/com.example.app D/ID:﹕ 30 05-02 07:09:00.556 
> 21204-21204/com.example.app D/Linija br:﹕ 000 05-02 07:09:00.556 
> 21204-21204/com.example.app D/i=﹕ 30 05-02 07:09:00.566 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.566 
> 21204-21204/com.example.app D/ID:﹕ 31 05-02 07:09:00.566 
> 21204-21204/com.example.app D/Linija br:﹕ 000 05-02 07:09:00.566 
> 21204-21204/com.example.app D/i=﹕ 31

有人能帮助我解决我在这里做错了什么吗?我会非常感激,我在这个上面花了很多时间尝试各种没有结果的东西。

编辑:

我添加了布局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="wrap_content"
    android:background="#ffffff"
    android:id="@+id/item_container"
    android:clickable="false"
    android:focusable="false">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:padding="2dp"
        android:background="#e8e8e8"
        android:id="@+id/item_body"
        android:clickable="false"
        android:focusable="false">

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/background"
            android:id="@+id/item_button"
            android:clickable="true"
            android:longClickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:minHeight="55dp">

                <FrameLayout
                    android:layout_width="3dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/transparent"
                    android:id="@+id/item_indicator"></FrameLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="141-SAMOBOR-RAKOV POTOK-JAGNJIĆ DOL"
                    android:id="@+id/item_text"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="30dp"
                    android:textAppearance="@android:style/TextAppearance.Small"
                    android:gravity="fill" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/item_icon"
                    android:layout_gravity="center_vertical|right"
                    android:background="@android:color/transparent"
                    android:src="@drawable/plus_t"
                    android:layout_marginRight="5dp"
                    android:contentDescription="Proširi" />

            </FrameLayout>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/item_image_container"
            android:layout_below="@+id/item_body"
            android:layout_marginTop="5dp"
            android:background="#ffffff">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/item_image"
                android:background="@android:color/transparent"
                android:layout_centerHorizontal="true"
                android:scaleType="fitCenter" />
        </RelativeLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

试试这种方式,希望这会对你有帮助......

XML 代码

<强> activity_main

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

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:src="@drawable/ic_launcher"
        android:adjustViewBounds="true"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/lnrDynamicItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        </LinearLayout>
    </ScrollView>

</LinearLayout>

ACTIVITY 代码

<强> MainActivity

  public class MainActivity extends Activity {

    private LinearLayout lnrDynamicItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lnrDynamicItem = (LinearLayout) findViewById(R.id.lnrDynamicItem);

        for (int i = 0; i <= id_l; i++)
        {
            Integer l = id_l;
            Log.d("id length: ", l.toString());
            Log.d("ID: ", id[i].toString());
            Log.d("Linija br: ", broj_linije[i]);
            Integer i2 = i;
            Log.d("i= ", i2.toString());

            item_layout.setId(i);
            item_layout.setTag("View");


            TextView textView = new TextView(this);
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView.setId(i+1);

            if(broj_linije[i].equals("000"))
            {
                textView.setText(naziv_linije[i]);
            }
            else
            {
                textView.setText(broj_linije[i] + " - " + naziv_linije[i]);
            }


            textView.setTag(textView.getId());



            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view)
                {
                    Integer a =  (Integer) view.getTag();
                    Toast.makeText(getApplicationContext(), "Clicked me", Toast.LENGTH_SHORT).show();
                    Log.d("clicked", h.toString());
                }
            });

            lnrDynamicItem.addView(textView);
        }

    }

 }