【问题标题】:Add a list of TextViews and ImageViews to an existing RelativeView programmatically以编程方式将 TextViews 和 ImageViews 列表添加到现有的 RelativeView
【发布时间】:2015-08-16 22:19:20
【问题描述】:

我有一个RelativeLayout,ID 为relative_layout_bottom,我正在尝试以编程方式向其中添加内容。我正在遍历 <TextView, ImageView> 字典并添加如下条目:

我希望RelativeLayout 的左侧(对齐父级左侧)有一个向下的TextViews 列表,而RelativeLayout 的右侧(对齐父级右侧)有一个@987654327 列表@像这样往下走:

|TextView#1               ImageView#1|
|TextView#2               ImageView#2|
|TextView#3               ImageView#3|
|TextView#4               ImageView#4|
|    .                         .     |
|    .                         .     |
|    .                         .     |

我似乎无法弄清楚如何做到这一点,但没有任何结果。如果有人愿意,我可以在我目前拥有的代码中进行编辑,但我很确定提出不同的解决方案比修复我的解决方案更有用。

这是我的 xml,我只使用了一个要附加的 TextView 和 ImageView?不确定它是否像那样工作,或者我是否应该以编程方式为每个条目创建一个单独的 TextView 和 ImageView 。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
tools:context="com.brettrosen.atls.activity.Report">

<LinearLayout
    android:id="@+id/report_top_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/scroll_view_top"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/checkbox_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"/>


        </RelativeLayout>

    </ScrollView>


 </LinearLayout>

<LinearLayout
    android:id="@+id/report_bottom_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/scroll_view_bottom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/relative_layout_bottom"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/checkbox_text_bottom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"/>

            <ImageView
                android:id="@+id/checkbox_notes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"/>
                />



        </RelativeLayout>

    </ScrollView>


</LinearLayout>

这是我目前的代码:

 TextView checkbox_text_bottom = (TextView) findViewById(R.id.checkbox_text_bottom);
    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout_bottom);

for (Map.Entry entry : PrearrivalPlan.imageButtonsWithNotes.entrySet()) {

        ImageButton imageButton = (ImageButton) entry.getValue();
        Bitmap bitmap = drawableToBitmap(imageButton.getBackground());

        checkbox_text_bottom.append(entry.getValue() + "\n"); // Append the text to the TextView

        ImageView image = new ImageView(Report.this); // Create an imageview
        image.setImageBitmap(bitmap);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)image.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        image.setLayoutParams(params);

        relativeLayout.addView(image); // Add it to the layout


    }

【问题讨论】:

  • @Heyyou 会做的,我会用我所做的来编辑我的帖子,但请记住它不起作用。

标签: android android-layout textview android-imageview


【解决方案1】:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">

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

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

                <LinearLayout
                    android:id="@+id/LinLayLeft"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/LinLayRight"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:orientation="vertical">

                </LinearLayout>
            </RelativeLayout>
        </ScrollView>
    </RelativeLayout>

Now u can add your widgets in the respective linearlayout using addview.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 2011-10-15
    • 1970-01-01
    相关资源
    最近更新 更多