如何在线性布局中以编程方式在我的图像下添加textview?

时间:2016-03-29 10:30:35

标签: android

任何人都可以帮我解决这个问题。

我想在线性布局中在我的图像下添加textview。

使用java类中的代码。

这是我的代码

for (int ir = 0; ir < jsonarrayProductsRecent.length(); ir++) {
    HashMap<String, String> map = new HashMap<String, String>();
    JSONObject pr = jsonarrayProductsRecent.getJSONObject(ir);

    ImageView thumbView = new ImageView(getActivity());
    Display display = getActivity().getWindowManager().getDefaultDisplay();
    int width = ((display.getWidth()*50)/100); // ((display.getWidth()*20)/100)
    int height = display.getHeight();// ((display.getHeight()*30)/100)
    LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.WRAP_CONTENT);
    //LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
    thumbView.setLayoutParams(parms);
    thumbView.setAdjustViewBounds(true);
    thumbView.setPadding(0, 0, 10, 0);
    Glide.with(getActivity())
            .load(pr.getString("products_image")).diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(thumbView);
    TextView tv= new TextView(getActivity());
    tv.setText("XYZ");
    layout.addView(thumbView);
    layout.addView(tv);

}

下面的代码是我的xml布局

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="horizontal"
    android:id="@+id/horizontalScrollView"
    android:fillViewport="false">

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"/>


</HorizontalScrollView>

2 个答案:

答案 0 :(得分:0)

试试这个

for (int ir = 0; ir < jsonarrayProductsRecent.length(); ir++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject pr = jsonarrayProductsRecent.getJSONObject(ir);

ImageView thumbView = new ImageView(getActivity());
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = ((display.getWidth()*50)/100); // ((display.getWidth()*20)/100)
int height = display.getHeight();// ((display.getHeight()*30)/100)
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,LinearLayout.LayoutParams.WRAP_CONTENT);
thumbView.setLayoutParams(parms);
thumbView.setAdjustViewBounds(true);
thumbView.setPadding(0, 0, 10, 0);
Glide.with(getActivity())
        .load(pr.getString("products_image")).diskCacheStrategy(DiskCacheStrategy.ALL)
        .into(thumbView);
llImage.addView(thumbView);     
TextView tv= new TextView(getActivity());
tv.setText("XYZ");
LinearLayout.LayoutParams captionViewlayoutParams = new LinearLayout.LayoutParams(
                    width, height);
tv.setLayoutParams(captionViewlayoutParams);
llCaption.addView(tv);
}

and change Your xml to this

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

                <LinearLayout
                    android:id="@+id/llImage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/llCaption"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:orientation="horizontal" >
                </LinearLayout>
            </LinearLayout>
               </HorizontalScrollView>

答案 1 :(得分:0)

在代码中添加以下行:

layout.setOrientation(LinearLayout.VERTICAL);