簡體   English   中英

對齊imageView和textView(以及帶邊框的溢出文本)

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

我已經看到很多方法可以將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 ...

Oooppsss! 現在布局是這樣的:

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

*父級的可見性並不重要,我以編程方式更改它。

謝謝。

好的...經過艱苦的實驗,搜索 - >嘗試 - >失敗 - >搜索 - >嘗試 - >失敗 - >搜索 - >嘗試 - >成功;)在這個堆棧問題中, @ K_Anas給出了解決方案......在這種情況下,我將為下一個案例鍵入我所做的事情(此外,可以處理文本上的鏈接,使用Html.fromHtml解析它並保留SpannableString之后的link屬性):

布局

 <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的meassures。

暫無
暫無

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

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