コーディング楽しみます他のビューが残りの空間に収まるかどうかを計算します。
ここでは2つのテキストビューでサンプルを作成しましたが、希望のビューに適用できます。 xmlは非常にまっすぐ遠く、普通のものではありません。テキスト1とテキスト2のレイアウトにに描かれた後
<?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>
ここでキャッチ、私はテキスト2の幅と一緒にテキスト1の最後の行に達しないところ計算し、そしてだ - それが合うことができるかどうかを確認し、画面の幅。
- text1とtext2を描画した後で、幅と右の位置を確認できるので、setText2Positionの両方でそれらを待っています。
- ここでキャッチするのは、上記の計算に従ってtext2にルールを追加することです。あなたの意見に余白とパディングを持っている場合は
- は、それを計算する必要があり、同様 FE、テキスト2は、パディングを持っている場合、チェックは次のようになります。
lastLineRight + text2.getWidth()+テキスト2。
: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のです
出典
2016-06-02 16:11:13
Dus
かなり曖昧ですが、文字が収まる残りのスペースやdpの実際の測定値をテキストからTextViewの範囲にしたいですか? – mastrgamr
スペースはpxまたはdpで、文字ではない – shilch
テキストの終わりまでのスペースを計算したいのですか? – umerk44