簡體   English   中英

如何以中間底部的Imageview和textview以編程方式生成水平滾動LinearLayout

[英]How to generate programmatically Horizontal scrolling LinearLayout with Imageview and textview at center bottom

我想以編程方式生成水平滾動的LinearLayout,其中Imageview和textview位於中心底部。

這是我的Java代碼:

      LinearLayout rec=(LinearLayout)findViewById(R.id.hori_recom);

     //ViewGroup.LayoutParams params=new   ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
       ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(450,450);
      ViewGroup.LayoutParams params1=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
      // for(int g=0;g<5;g++)
      for(int g=0;g<imgpath_orignal.size();g++)
      {

        ImageView recimg=new ImageView(Details.this);
        recimg.setId(g+1);
        recimg.setPadding(25,0,0,0);
        Picasso.with(Details.this).load(al_gallary_img.get(g)).placeholder(R.drawable.logo)
                .error(R.drawable.logo).into(recimg);
        recimg.setLayoutParams(params);
        TextView txtlabel=new TextView(Details.this);
        txtlabel.setId(g+1);
        txtlabel.setGravity(Gravity.CENTER|Gravity.BOTTOM);
        txtlabel.setPadding(15,15,15,15);
        txtlabel.setText(""+al_img_caption.get(g));
        txtlabel.setLayoutParams(params1);
        rec.addView(txtlabel);
        rec.addView(recimg);

這是我的Xml:

           <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="none">


                 <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:id="@+id/hori_recom"
                    android:layout_marginTop="10dp"/>

         </HorizontalScrollView>

問題是我的textview不在Imageview的底部中心。

這是因為您已將TextView的重力設置為center_bottom而不是layout_gravity。

只需刪除

txtlabel.setGravity(Gravity.CENTER|Gravity.BOTTOM);

而不是

ViewGroup.LayoutParams params1 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);`

做這個

LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); params1.gravity = Gravity.CENTER|Gravity.BOTTOM;

這是通過編程設置layout_gravity的方法。

做一件事-在一個布局中組合ImageViewTextView
將此布局稱為row_layout

<LinearLayout
    ....
    ....

    <ImageView
        ...
        ... />
    <TextView
        ...
        ... />
</LinearLayout>

現在添加布局到LinearLayout說謊成HorizontalScrollView -

for(int g=0; g<imgpath_orignal.size(); g++) {
    View v = LayoutInflater.from(context).inflate(R.layout.row_layout, parent, false);
    // Do your layout binding stuff over here..
    ImageView ivPhoto = (ImageView) v.findViewById(R.id.imageview_id); // Give reference
    Picasso.with(context).load(url).into(ivPhoto); // Loading image using Picasso
    rec.addView(v);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM