Android 对齐imageView和textView(并使文本溢出边框)
我已经看到了许多将文本视图与图像视图对齐的方法,但是我没有找到如何在文本大于图像时,以及当您希望文本占据所有父布局时进行对齐。我的布局如下:Android 对齐imageView和textView(并使文本溢出边框),android,android-layout,textview,imageview,alignment,Android,Android Layout,Textview,Imageview,Alignment,我已经看到了许多将文本视图与图像视图对齐的方法,但是我没有找到如何在文本大于图像时,以及当您希望文本占据所有父布局时进行对齐。我的布局如下: <RelativeLayout android:id="@+id/accordion_toogle" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" a
<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
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>
但是没有显示图像,我尝试了很多其他方法,比如:
textview.setCompoundDrawablesRelative(null, draw, null, null);
并且图像保持在文本的顶部。我尝试的其他不同方法是:
textview.setCompoundDrawables(draw, null, null, null);
图像在左边。。。但它是垂直对齐的中心,顶部和底部的图像是空白的,并没有文字,并没有图片。。。没什么。我尝试将父对象更改为LinearLayout,使用重力左、重力上、相对长度
哦!!!现在布局如下:
<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
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属性: 布局
在这种情况下,宽度和高度是ImageView的测量值。恐怕这会令人困惑。如果你可以添加你正在得到的和你想要的图像,这将非常有帮助。我自己有这样的控制,我更喜欢使用线性布局,而不是在父布局上设置重力属性,以便子布局符合。
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);