简体   繁体   English

Android-以编程方式实现视图

[英]Android - Implement Views programmatically

I want to implement an imageview and a textview side by side. 我想并排实现imageview和textview。 I achieved this by using XML. 我是通过使用XML实现的。 However i wanted to achieve this programmatically but had no luck so far. 但是我想以编程方式实现这一目标,但到目前为止还没有运气。 My XML and Java code are below. 我的XML和Java代码如下。 Please help me to execute programmatically. 请帮助我以编程方式执行。

I'm executing the Java code in a fragment. 我正在片段中执行Java代码。

XML CODE: XML代码:

<RelativeLayout
       android:id="@+id/rl1"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >

    <com.loopj.android.image.SmartImageView
        android:id="@+id/my_image1"
        android:layout_width="160dip" 
        android:layout_height="100dip"
        android:adjustViewBounds="true"  
        android:scaleType="fitXY" 
         />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/my_image1"
        android:layout_alignTop="@+id/my_image1"
        android:layout_toRightOf="@+id/my_image1"
        android:gravity="center_vertical"
     />
</RelativeLayout>

JAVA CODE: JAVA代码:

RelativeLayout rl1 = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams rlParams;
rlParams = new RelativeLayout.LayoutParams(
                 newLayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rlParams.addRule(LinearLayout.VERTICAL);
rl1.setLayoutParams(rlParams);

SmartImageView siv1 = new SmartImageView(getActivity());
siv1.setId(rand.nextInt(50000) + 1);
siv1.setLayoutParams(new LayoutParams(width,height));
siv1.setAdjustViewBounds(true);
siv1.setScaleType(ScaleType.FIT_XY);
siv1.setImageUrl(Uri);

TextView tv1 = new TextView(getActivity());
RelativeLayout.LayoutParams relativeLayoutParams;
relativeLayoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(relativeLayoutParams);
relativeLayoutParams.setMargins(10, 0, 0, 0);
tv1.setGravity(Gravity.CENTER_VERTICAL);
tv1.setText("Sample Text");

rl1.addView(siv1);
rl1.addView(tv1);
ll.addView(rl1);

By executing the above code, i'm getting the image but the text is inside the image. 通过执行以上代码,我得到了图像,但文本在图像内部。 But, i want to get the image on the left and the text on the right. 但是,我想在左侧获得图像,在右侧获得文本。

Thanks in advance 提前致谢

Add below code to your Activity: 将以下代码添加到您的活动中:

rlParams.addRule(RelativeLayout.ALIGN_TOP, siv1);
rlParams.addRule(RelativeLayout.ALIGN_BOTTOM, siv1);
rlParams.addRule(RelativeLayout.RIGHT_OF, siv1);
tv1.setLayoutParams(rlParams);

and then do: 然后执行:

rl1.addView(siv1);
rl1.addView(tv1);

Hope this helps. 希望这可以帮助。

In your case it is better to use a horizontal LinearLayout : 在您的情况下,最好使用水平LinearLayout

LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(param);

TextView textview = new TextView(getActivity());
...



SmartImageView siv1 = new SmartImageView(getActivity());
...

ll.addView(textview);
ll.addView(siv1);

To align the views in RelativeLayout is much difficult. 在RelativeLayout中对齐视图非常困难。 I will suggest you to use the TableRows or LinearLayouts. 我建议您使用TableRows或LinearLayouts。 Both these give much easier way to align views side by side. 这两种方式都可以更轻松地将视图并排对齐。 Here is a source in which one TextView is aligned-left with 4 ImageViews aligned on its right. 这是一个Source,其中一个TextView左对齐,其右对齐4个ImageView。 You can get idea from this TextView taking/consuming too much space above and below the text 您可以从此TextView中获得想法, 在文本上方和下方占用/占用过多空间

You can set the margin of your text like this 您可以这样设置文本的边距

relativeLayoutParams.setMargins(200, 0, 0, 0);

but this is not the best practice so do one thing take linear layout set orientation to horizontal add both imageview and textview to it and then add this linearlayout to your relative layout. 但这不是最佳实践,因此请采取一件事,将线性布局设置方向设置为水平,同时将imageview和textview都添加到其中,然后将此线性布局添加到您的相对布局中。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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