[英]TextView - Move view to end
我有多行TextView
。 如何確定最后的空間以了解視圖是否適合其中?
例:
--------------------
| I am a multiline |
| text that has |
| space at the |
| end. <View>|
--------------------
--------------------
| I am a multiline |
| text that has |
| not enough space |
| at theeee end. |
| <View>|
--------------------
因此,我希望視圖位於右下角,它不應覆蓋文本,但如果有足夠的空間,則應覆蓋在文本的右邊
基本上,您需要查看最后一行的右邊(y坐標)到達何處,然后計算其他視圖是否可以容納其余空間。
我在這里用2個textviews做了一個示例,但是它可以應用於您想要的任何視圖。 xml非常直截了當,與眾不同。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kasjdf as df asd aasdfasdff as dfa sd sdf as df asd fa sd fa sdf as dfanas,df askdjfasjdf lkajsldkf jlaksjdfajsdkfj alskdj fklasjdflkasjdlkfdflk ajskldf" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="attached text"
android:textColor="@android:color/holo_blue_dark" />
</RelativeLayout>
在這里要抓住的是,在布局中將text1和text2繪制到后,我正在計算text1的最后一行到達的位置,以及text2的寬度-看看它是否適合屏幕的寬度。
lastLineRight + text2.getWidth()+ text2.getPaddingRight()+ text2.getPaddingLeft()> screenWidth
public class MainActivity extends AppCompatActivity {
private boolean[] areBothViewsDrawn = {false, false};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text1 = (TextView) findViewById(R.id.text1);
final TextView text2 = (TextView) findViewById(R.id.text2);
text1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
text1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
areBothViewsDrawn[0] = true;
setText2Position(text1, text2);
}
});
text2.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
text2.getViewTreeObserver().removeGlobalOnLayoutListener(this);
areBothViewsDrawn[1] = true;
setText2Position(text1, text2);
}
});
}
private void setText2Position(TextView text1, TextView text2) {
if (!areBothViewsDrawn[0] || !areBothViewsDrawn[1])
return;
int lineCount = text1.getLayout().getLineCount();
float lastLineRight = text1.getLayout().getLineRight(lineCount - 1);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text2.getLayoutParams();
if (lastLineRight + text2.getWidth() > screenWidth) {
//The width of the last line of text1 is too long for the text2 to fit, so text2 must be in another line
params.addRule(RelativeLayout.BELOW, text1.getId());
} else {
//The width of the last line of text1 and text2 smaller then the application's width, so text2 can be in the same line as text1
params.addRule(RelativeLayout.ALIGN_BOTTOM, text1.getId());
}
}
}
這是我的代碼的一些POC:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.