简体   繁体   中英

How to convert xml positioning to java code?

I would like to show all avaliable seances from database in a specific way. Here is what i designed in creator.

在此处输入图片说明

I would like to show undefined number of movies so i generate this in java. How to convert xml positioning to java? Which classes to use? Here is xml file:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="78dp"
        android:layout_gravity="center_horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Spectre (2015)"
            android:id="@+id/textView"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Great Britain 2D Subtitles 121 min"
            android:id="@+id/textView2"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="18:45"
            android:id="@+id/textView3"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
</RelativeLayout>

Here is java code:

for (int i = 0; i < 20; i++) {
        RelativeLayout relLayout = new RelativeLayout(this);
        TextView textView = new TextView(this);
        textView.setText("Big black text:" + i);
        textView.setTextSize(22);
        textView.setTextColor(Color.BLACK);
        TextView textView1 = new TextView(this);
        textView1.setText("Middle grey text:"+i);
        textView1.setTextSize(18);
        textView1.setTextColor(Color.GRAY);
        TextView textView2 = new TextView(this);
        textView2.setText("Right down corner:" + i);
        textView2.setTextSize(20);
        textView2.setTextColor(Color.BLACK);
        relLayout.addView(textView);
        relLayout.addView(textView1);
        relLayout.addView(textView2);
        relLayout.setId(i);
        final int id_ = relLayout.getId();

        LinearLayout layout = (LinearLayout) findViewById(R.id.llayout);
        layout.addView(relLayout);

        relLayout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(),
                        "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                        .show();
            }
        });
    }

Take a look at the following class

android.widget.ListView

Your approach is wrong, because if you have a lot of rows, the app will simply crash.

And of course you want custom list item which is generally a popular topic you can google out, but here is one of them:

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM