简体   繁体   English

如何在LinearLayout的同一行上设置Imageview和Textview

[英]How can I set Imageview and Textview on same line in LinearLayout

I want to set Imageview and Textview on same line in LinearLayout but the Imageview is always higher than the Textview. 我想在LinearLayout的同一行上设置Imageview和Textview,但是Imageview总是高于Textview。

Here is my code: 这是我的代码:

String b[] = cacBuocThucHien.split("#");
for (int j = 0; j < b.length; j++) {
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(30,30);
    lp.setMargins(50, 0, 0, 0);
    ImageView img = new ImageView(RecipesDetailActivity.this);
    TextView tv = new TextView(RecipesDetailActivity.this);
    img.setLayoutParams(lp2);
    img.setImageResource(R.mipmap.testic);
    tv.setLayoutParams(lp);
    tv.setText(b[j] + "\n");
    layoutHuongDan.addView(img);
    layoutHuongDan.addView(tv);
}

This You can use to put image in one line.. and it will work in all kind of layouts. 这可以用来将图像放在一行中。它可以在所有布局中使用。 Hope this will help you 希望这个能对您有所帮助

  <LinerLayout>     
   <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
  <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
   ....... // Put how many TextViews you want

 <TextView
        android:id="@+id/textn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 </LinerLayout>

You can set visibility gone initially if you wanna show these textviews only after setting text. 如果只想在设置文本后显示这些文本视图,则可以将可见性设置为最初不显示。 Now in your activity make an array of ids of textviews like this... 现在在您的活动中,像这样创建一个textview的ID数组...

public static int[] textRows = {R.id.text1, R.id.text2, ........ R.id.textn};

then use for loop for initializing them and setting text and images like this 然后使用for循环初始化它们,并像这样设置文本和图像

   TextView[] allTexts = new TextView[n];
   for (int i = 0; i < n; i++) {
        allTexts[i] = (TextView) findViewById(textRows[i]);
        allTexts[i].setText("your text");
        allTexts[i].setCompoundDrawables(left_image, null, null, null); 
    }

It will work. 它会工作。 Try it out 试试看

设置textview可绘制的左侧,无需额外的imageview

android:drawableLeft="@drawable/img"

If you only want to show an icon beside that TextView , in my opinion you should consider using drawable icon feature of TextView 如果您只想在该TextView旁边显示图标,我认为您应该考虑使用TextView 可绘制图标功能

tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

But if you would insist to using two views beside each other, using layout gravity would be useful in such circumstances. 但是,如果您要坚持使用彼此相邻的两个视图,则在这种情况下使用布局重力会很有用。

lp.gravity= Gravity.CENTER_VERTICAL;

If you want to display only TextView and ImageView .. go for setting CompounDrawable in TextView itself.. 如果只想显示TextViewImageView ..,请在TextView本身中设置CompounDrawable

But, for having two views side-by-side in LinearLayout programmatically try setting the orientation=horizontal , check out this link . 但是,要以LinearLayout方式在LinearLayout并排放置两个视图,请尝试设置LinearLayout orientation=horizontal ,请查看此链接

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

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