如何在Android中的表视图中的单个单元格中包装多个文本视图

时间:2014-02-22 21:22:48

标签: android android-layout android-linearlayout android-tablelayout

我需要在表格的单个单元格中包含一系列文本视图,但我无法弄清楚如何使文本视图保留在一个单元格中并根据整个文本视图是否适合同一行进行换行。每个类别名称都是指向另一个活动的链接,每个事件文本视图都是指向另一个活动的链接。此表也是动态构建的,因此可能会提供一些限制。

这是我试图确定需要设置的设置的测试布局。

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Category"
            android:id="@+id/textView3" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Events"
            android:id="@+id/textView4" />
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="A"
            android:id="@+id/textView5" />

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Event 1"
                android:id="@+id/textView6" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text=", Event 2"
                android:id="@+id/textView7" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/textView8"
                android:text=", Event 3" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text=", Event 4"
                android:id="@+id/textView9" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text=", Event 5"
                android:id="@+id/textView12"
                android:singleLine="true" />
        </TableRow>
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="B"
            android:id="@+id/textView10" />

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Event 1"
                android:id="@+id/textView11" />
        </TableRow>
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="C"
            android:id="@+id/textView"
             />

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tableRow">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Event 1"
                android:id="@+id/textView2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text=", Event 2"
                android:id="@+id/textView13" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/textView14"
                android:text=", Event 3"
                android:singleLine="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text=", Event 4"
                android:id="@+id/textView15" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text=", Event 5"
                android:id="@+id/textView16"
                android:singleLine="true" />
        </TableRow>
    </TableRow>

</TableLayout>

更新

仍然经过一周的摆弄,我仍然无法让我的桌子正确显示。我正确地解析了所有我个别的火山爆发,但仍然无法让它们像在我们的网页上那样换行到一条新线,因为单个喷泉的喷发次数超过了该线上的数量。以下是我们在Web界面上使用的该表的链接(如果您更改浏览器大小,您将了解我使用Android应用程序后的内容。)http://geysertimes.org/retrievedailylog.php?date=2013-06-24。这是我的代码,为目前的应用程序构建表。

List<Eruption> alleruptionssum = null;
    try {
        alleruptionssum = db.getDaySummary(CurDateStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    String GeyserOLD = "";
    String timeSTR = "";
    long GeyserIDOLD = 0;
    long numEruptions = 0;
    if (alleruptionssum != null) {
        if(alleruptionssum.size() !=0){
            for (Eruption eruption : alleruptionssum) {
                int Live;
                long GeyserID;
                long EruptionID;
                long EruptionUnixTime;
                long EruptionUnixTimeMilli;
                String EruptionRegTime;
                String EruptionTimeDisplay;
                String TimeInfo;
                String Geyser;

                Live = eruption.getLive();
                GeyserID = eruption.getGeyserID();
                EruptionID = eruption.getId();
                Geyser = eruption.getGeyser();
                EruptionUnixTime = eruption.getEruptionUnixtimeTime();
                EruptionUnixTimeMilli = EruptionUnixTime * 1000;
                Date EruptionDate = new Date(EruptionUnixTimeMilli);
                SimpleDateFormat EruptionTimeFormatter = new SimpleDateFormat("HHmm");
                EruptionTimeFormatter.setTimeZone(TimeZone.getTimeZone("US/Mountain"));
                EruptionRegTime = EruptionTimeFormatter.format(EruptionDate);

                TimeInfo = eruption.getTimeInfo();
                EruptionTimeDisplay = EruptionRegTime + TimeInfo;


                if(!GeyserOLD.equals(Geyser)){
                    TableRow trGeyser = new TableRow(this);
                    //Set geyser text
                    TextView tvGeyser = new TextView(this);
                    tvGeyser.setPaintFlags(tvGeyser.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                    tvGeyser.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
                    tvGeyser.setTextColor(Color.parseColor("#0000EE"));
                    tvGeyser.setBackgroundResource(R.drawable.cell_left_middle);
                    tvGeyser.setPadding(4, 2, 4, 2);
                    tvGeyser.setText(GeyserOLD);
                    final long finalGeyserID = GeyserIDOLD;
                    tvGeyser.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.d("GeyserID", String.valueOf(finalGeyserID));
                            link_recent_geyser_info(finalGeyserID);
                        }
                    });
                    trGeyser.addView(tvGeyser);

                    if(!timeSTR.equals("")){
                        LinearLayout llEruptions = new LinearLayout(this);
                        llEruptions.setBackgroundResource(R.drawable.cell_right_middle);
                        llEruptions.setPadding(4,2,4,2);
                        String[] eruptions = timeSTR.split(";");
                        for ( long j = 0; j < numEruptions; j++ ) {
                            String[] ParsedEruptions = eruptions[(int) j].split(",");
                            long PostErupionID = Long.parseLong(ParsedEruptions[0]);
                            String PostEruptionDisplay = ParsedEruptions[1];
                            int PostLive = Integer.parseInt(ParsedEruptions[2]);
                            TextView comma = new TextView(this);
                            comma.setText(", ");
                            comma.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
                            if(j != 0){
                                llEruptions.addView(comma);
                            }
                            TextView tvTime = new TextView(this);
                            tvTime.setText(PostEruptionDisplay);
                            tvTime.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
                            tvTime.setPaintFlags(tvTime.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                            if(PostLive == 0){
                                tvTime.setTypeface(null, Typeface.ITALIC);
                                tvTime.setTextColor(Color.parseColor("#FFBD00"));
                            }else{
                                tvTime.setTextColor(Color.parseColor("#0000EE"));
                            }
                            final long finalEruptionID = PostErupionID;
                            tvTime.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Log.d("EruptionID", String.valueOf(finalEruptionID));
                                    link_eruption_detail(finalEruptionID);
                                }
                            });
                            llEruptions.addView(tvTime);
                        }
                        trGeyser.addView(llEruptions);
                        SummaryDataView.addView(trGeyser);
                    }
                    GeyserOLD = Geyser;
                    GeyserIDOLD = GeyserID;

                    timeSTR = EruptionID+","+EruptionTimeDisplay+","+Live;
                    numEruptions = 1;

                }else{
                    //add 1 to eruption counter for current geyser and add the eruption to the string
                    numEruptions = numEruptions+1;
                    timeSTR = timeSTR + ";" + EruptionID+","+      EruptionTimeDisplay+","+Live;
                }
            }
            TableRow trGeyser = new TableRow(this);
            //Set geyser text
            TextView tvGeyser = new TextView(this);
            tvGeyser.setTextColor(Color.parseColor("#0000EE"));
            tvGeyser.setBackgroundResource(R.drawable.cell_left_bottom);
            tvGeyser.setPadding(4, 2, 4, 2);
            tvGeyser.setPaintFlags(tvGeyser.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
            tvGeyser.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
            tvGeyser.setText(GeyserOLD);
            trGeyser.addView(tvGeyser);

            LinearLayout llEruptions = new LinearLayout(this);

            llEruptions.setBackgroundResource(R.drawable.cell_right_bottom);
            llEruptions.setPadding(4,2,4,2);
            String[] eruptions = timeSTR.split(";");
            for ( long j = 0; j < numEruptions; j++ ) {
                String[] ParsedEruptions = eruptions[(int) j].split(",");
                long PostErupionID = Long.parseLong(ParsedEruptions[0]);
                String PostEruptionDisplay = ParsedEruptions[1];
                int PostLive = Integer.parseInt(ParsedEruptions[2]);
               //add comma between eruptions
                TextView comma = new TextView(this);
                comma.setText(", ");
                comma.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
                if(j != 0){
                    llEruptions.addView(comma);
                }
                TextView tvTime = new TextView(this);
                tvTime.setText(PostEruptionDisplay);
                tvTime.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
                if(PostLive == 0){
                    tvTime.setTypeface(null, Typeface.ITALIC);
                    tvTime.setTextColor(Color.parseColor("#FFBD00"));
                }
                final long finalEruptionID = PostErupionID;
                tvTime.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        link_eruption_detail(finalEruptionID);
                    }
                });
                llEruptions.addView(tvTime);
            }
            trGeyser.addView(llEruptions);
            SummaryDataView.addView(trGeyser);
        }
    }

0 个答案:

没有答案