【问题标题】:Align imageView and textView (and overflow text with border)对齐 imageView 和 textView (并溢出带有边框的文本)
【发布时间】:2015-04-20 05:58:10
【问题描述】:

我已经看到了许多将 textview 与 imageview 对齐的方法,但我没有找到如何使文本大于图像,以及当您希望文本占据所有父布局时。我的布局是这样的:

<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>

在这种情况下,假设图像占据了RelativeLayout的一半,一旦imageView完成,accordion_msg继续占据relativeLayout的一半。

你懂我吗??我表达得好吗??有什么建议吗??

谢谢。

编辑 一张图片可以帮助我尝试做的事情:

粉红色的方块是图片,紫色和白色的矩形是文字必须在的地方,但到目前为止,紫色是空的。文字只占据白色方块。

编辑 2

我正在尝试@Elltz 所说的......但我无法做出我想要的,我有这个代码:

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

但没有显示图像,我尝试了其他很多东西,例如:

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

并且图像停留在文本的顶部。我尝试过的其他不同之处是:

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

并且图像在左侧......但它在中心垂直对齐,并且图像的顶部和底部是空白的(没有文字,没有图片......没有任何东西)。我尝试将父级更改为 LinearLayout,使用重力左、重力顶部、RelativeLayout...

哎呀!现在布局是这样的:

<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>

*父级上的可见性并不重要,我以编程方式更改它。

谢谢。

【问题讨论】:

  • 恐怕这会令人困惑。如果你能添加你正在得到的和你想要的图像,那会很有帮助。
  • 我自己拥有这样的控制权,我更喜欢使用 LinearLayouts 并在父布局上设置 Gravity 属性,以便子布局符合。

标签: android android-layout textview imageview alignment


【解决方案1】:

好的...经过艰苦的实验,搜索->尝试->失败->搜索->尝试->失败->搜索->尝试->成功;) 在this stack question@K_Anas 中给出了解决方案......我将在这种情况下输入我为下一个案例所做的事情(此外,可以处理文本上的链接,使用 Html.fromHtml 解决它并在之后保留链接属性SpannableString):

布局

 <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>

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);

在这种情况下,宽度和高度是 ImageView 的度量值。

【讨论】:

    猜你喜欢
    • 2021-10-23
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 2015-01-08
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多