简体   繁体   English

对齐imageView和textView(以及带边框的溢出文本)

[英]Align imageView and textView (and overflow text with border)

I had saw many ways to align a textview with an imageview, but I didn´t find how to make when the text is bigger than the image, and when you want that text occupy all the parent layout. 我已经看到很多方法可以将textview与imageview对齐,但是当文本比图像大时,以及当你希望文本占据所有父布局时,我都没有找到。 My layout is this: 我的布局是这样的:

<RelativeLayout
    android:id="@+id/accordion_toogle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="gone">
    <ImageView
        android:id="@+id/accordion_image"
        android:layout_width="@dimen/accor_img_wid"
        android:layout_height="@dimen/accor_img_hei" />

    <TextView
        android:id="@+id/accordion_msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/accordion_image"
        android:paddingLeft="5dp"/>
</RelativeLayout>

In this case, assuming that the image occupy the half of the RelativeLayout, the accordion_msg continues to occupy the half of the relativeLayout once the imageView finish. 在这种情况下,假设图像占据RelativeLayout的一半,一旦imageView完成,accordion_msg继续占据relativeLayout的一半。

Do you understand me?? 你了解我吗?? Have I expressed well?? 我表达得好吗? Any suggestion?? 有什么建议??

Thank you. 谢谢。

EDIT A picture to help what I try to do: 编辑一张照片,以帮助我尝试做的事情: 理想的设计

The pink square is the picture, the purple and white rectangles are where the text must to be, but until now, the purple is empty. 粉红色正方形是图片,紫色和白色矩形是文本必须的位置,但直到现在,紫色是空的。 The text only occupy the white square. 文字只占据白色方块。

EDIT 2 编辑2

I am trying what @Elltz says... but I cannot make what I want, I have this code: 我正在尝试@Elltz所说的......但我无法做出我想要的东西,我有这个代码:

draw.setBounds(0, 0, width, height); //width: 128 height: 172 in this case
textview.setCompoundDrawablesRelative(draw, null, null, null);

But no image is shown, I try other many things, like: 但是没有显示图像,我尝试了很多其他的东西,比如:

textview.setCompoundDrawablesRelative(null, draw, null, null);

And the image stays on the top of the text. 图像保留在文本的顶部。 Other diferent this I tried was: 我试过的其他不同之处是:

textview.setCompoundDrawables(draw, null, null, null);

And the image is on the left... but it is vertically align at center, and top and bottom of image are blank (no text, no picture... no nothing). 图像在左侧......但它在中心垂直对齐,图像的顶部和底部是空白的(没有文字,没有图片......没有任何东西)。 I try changing the parent to LinearLayout, using gravity left, gravity top, RelativeLayout... 我尝试将父级更改为LinearLayout,使用重力左,重力顶部,RelativeLayout ...

Oooppsss!!! Oooppsss! now the layout is this: 现在布局是这样的:

<RelativeLayout
    android:id="@+id/accordion_toogle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone">
    <TextView
        android:id="@+id/accordion_msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/accordion_image"
        android:paddingLeft="5dp"/>
</RelativeLayout>

*The visibility gone on the parent is not important, I change it programmatically. *父级的可见性并不重要,我以编程方式更改它。

Thank you. 谢谢。

Ok... after a hard job of experimentation, searching-> attempting-> failing-> searching-> attempting-> failing-> searching-> attempting-> success ;) In this stack question @K_Anas gives the solution... I will type what I do for the next case on this situation (Furthermore, it is possible process a link on the text, resolving it with Html.fromHtml and preserving the link property after the SpannableString): 好的...经过艰苦的实验,搜索 - >尝试 - >失败 - >搜索 - >尝试 - >失败 - >搜索 - >尝试 - >成功;)在这个堆栈问题中, @ K_Anas给出了解决方案......在这种情况下,我将为下一个案例键入我所做的事情(此外,可以处理文本上的链接,使用Html.fromHtml解析它并保留SpannableString之后的link属性):

The Layout 布局

 <RelativeLayout
    android:id="@+id/accordion_toogle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="6dp"
    android:visibility="gone"
    android:gravity="top">

    <ImageView
        android:id="@+id/accordion_image"
        android:layout_width="@dimen/img_width"
        android:layout_height="@dimen/img_height"
        android:src="@drawable/default_image"/>
    <TextView
        android:id="@+id/accordion_msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"/>
</RelativeLayout>

The Java code Java代码

final TextView msgView = (TextView)view_aux.findViewById(R.id.accordion_msg);
String msg_text = dbStructure.getMsg();
if(msg_text.contains("href=\"")) {
    String[] msg_aux = msg_text.split("href=\"");
    if (!msg_aux[1].toLowerCase().startsWith("http"))
        msg_aux[1] = "http://" + msg_aux[1];
    msg_text = msg_aux[0] + "\"href=\"" + msg_aux[1];
}

msgView.setText(Html.fromHtml(msg_text));
msgView.setMovementMethod(LinkMovementMethod.getInstance());

int lines = (int)Math.round(height / msgView.getLineHeight());
SpannableString ss = new SpannableString(msgView.getText());
ss.setSpan(new MyLeadingMarginSpan2(lines, width+10), 0, ss.length(), 0);

msgView.setText(ss);

In this case the width and height are the meassures of the ImageView. 在这种情况下,宽度和高度是ImageView的meassures。

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

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