我想显示文字左和下图像。如何让文字左和下图像
+---------------------------+
| Text................ Image|
+.......(remain text)........+
我想显示文字左和下图像。如何让文字左和下图像
+---------------------------+
| Text................ Image|
+.......(remain text)........+
我用这个代码
String text = getResources().getString(R.string.text);
SpannableString ss = new SpannableString(text);
ss.setSpan(new MyLeadingMarginSpan2(6, 230), 0, ss.length(), 0);
TextView textview = (TextView) view.findViewById(R.id.descriptionTextView);
textview.setText(ss);
class MyLeadingMarginSpan2 implements LeadingMarginSpan.LeadingMarginSpan2 {
private int margin;
private int lines;
MyLeadingMarginSpan2(int lines, int margin) {
this.margin = margin;
this.lines = lines;
}
@Override
public int getLeadingMargin(boolean first) {
if (first) {
return margin;
} else {
return 0;
}
}
@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
int top, int baseline, int bottom, CharSequence text,
int start, int end, boolean first, Layout layout) {}
@Override
public int getLeadingMarginLineCount() {
return lines;
}
}
您可以在布局XML文件中做到这一点,只需添加到ImageView的这个属性:
android:layout_toRightOf="@id/text"
,这将使它的文字的右侧,然后使它们之间的距离使用这个属性:
android:layout_marginRight="70dp"
而对于其他的文字使用toRightOf代替 - android:layout_below="@id/text"
,然后保持距离,再次使用marginRight屏幕的起点。与此分配正确的文本和图像的ID。
可能出现的代码编辑:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@android:drawable/alert_dark_frame"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>
请注意,layout_toRightOf只适用于RelativeLayout –
@DavidBalas谢谢你有一个只有文字,我想在图像下完成 –
@FrankD。谢谢,其实我不知道。此外,艾哈迈德,我正在编辑我的答案,可能有完整的代码和结果。 – DavidBalas
@Ahmed穆罕默德:我不知道它会帮助你或没有。您可以使用FlowTextView .A TextView that extends RelativeLayout
。文本将环绕布局中的任何子视图。
请结帐这个Git演示。如果不工作通知我。 FlowTextView
谢谢,它是一个很好的图书馆,它运行良好。 –
请显示您尝试过的代码 –
您尝试过什么吗? –
可能的重复 - http://stackoverflow.com/questions/2248759/how-to-layout-text-to-flow-around-an-image – F43nd1r