अंतिम परिणाम
स्पष्टीकरण
यह किसी भी तरह से सही नहीं है और इसकी ज़रूरतों को पूरा करने के लिए इसके लिए कुछ बदलाव की आवश्यकता होगी। मैंने इसे अधिक सामान्य बनाने के लिए ellipsize
भाग छोड़ा, लेकिन इसके लिए कोड को अनुकूलित करना आसान होगा। विचार सरल है लेकिन इसे काम करने के लिए मुझे कुछ घंटों (केवल इसलिए कि रविवार) ले गया। हम गणना करते हैं कि दूसरे TextView
से कितने अधिक वर्ण पहले की अंतिम पंक्ति में फिट होंगे। फिर उस संख्या के साथ हम कंटेनर की पृष्ठभूमि से मेल खाने वाले रंग के साथ दूसरे TextView
पर अंतिम पंक्ति जोड़ते हैं, यह एक ऐसी सीमा है जिसे शायद किसी भी तरह हल किया जा सकता है, लेकिन मुझे यह प्रतीत नहीं होता है।
इसके साथ, हमारे पास उनके टेक्स्ट गठबंधन के साथ दो विचार होंगे, फिर भी वे रैप प्रभाव की अनुमति देने के लिए एक-दूसरे को ओवरलैप नहीं कर रहे हैं। इसलिए हमें पाठ की ऊंचाई मिलती है और दूसरे को नकारात्मक मार्जिन जोड़ दिया जाता है ताकि वे यह सुनिश्चित कर सकें कि पहले TextView
दूसरे के शीर्ष पर है, हमें उसी रंग के साथ पाठ रखने से बचने के लिए bringToFront()
पर कॉल करने की आवश्यकता है पहले की आखिरी पंक्ति पर पृष्ठभूमि (वाह यह भ्रमित हो रहा है)। यह काम नहीं कर सकता विभिन्न फ़ॉन्ट्स और वजन (उदाहरण के लिए बोल्ड) के साथ अच्छा है क्योंकि पात्रों की संभावना अधिक जगह लेगी।
मैंने इसे एक विधि नहीं बनाया है लेकिन यह आसानी से सामान्य और पुन: प्रयोज्य हो सकता है, सिर्फ यह साबित करना चाहता था कि मैं इसे कर सकता हूं या नहीं। यहां किसी के लिए कोड और हर किसी का आनंद लेने और सुधार करने के लिए कोड है। लेआउट बस एक कंटेनर है (उदाहरण के लिए RelativeLayout
) और आईडी first
और second
के साथ दो टेक्स्टव्यू, मैंने इसे स्पष्ट करने के लिए दूसरे रंग पर लाल रंग को लाल रंग सेट कर दिया है लेकिन यह आवश्यक नहीं है। FontMetrics http://docs.oracle.com/javase/8/docs/api/java/awt/FontMetrics.html इस वर्ग के हम इतने पर वर्ण, तार को मापने और कर सकते हैं:
कोड
final TextView first = (TextView) findViewById(R.id.textView);
final TextView second = (TextView) findViewById(R.id.textView2);
final ViewGroup container = (ViewGroup)findViewById(R.id.container);
final ViewTreeObserver obs = findViewById(R.id.container).getViewTreeObserver();
obs.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//Get characters from the first TextView's last line
String lastLine = first.getText().toString().substring(first.getLayout().getLineStart(first.getLineCount() - 1));
//Calculate the number of extra characters that will fit in the first one
int e = 0; //Extra characters
int lineCount = first.getLineCount();
while (lineCount == first.getLineCount() && e < second.getText().toString().length() - 1) {
first.append(second.getText().toString().substring(e, e + 1));
e++;
}
//Remove those characters again to leave the first TextView as it was
String firstString = first.getText().toString();
first.setText(firstString.substring(0, firstString.length() - e));
//Add the last line of the first textview to the second textview
second.setText(Html.fromHtml("<font color='#F2F2F2'>"+lastLine+"</font>"+second.getText()));
//add a negative margin to the second textview equal to a line's height
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) second.getLayoutParams();
params.topMargin = -(int)(first.getLineHeight()+10);
second.setLayoutParams(params);
//Make sure first is on top
first.bringToFront();
//Remove the listener
if (Build.VERSION.SDK_INT < 16) {
container.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
container.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
स्रोत
2014-09-28 19:06:44
उपयोग कर सकते हैं क्या आपका मतलब है आप BBB के लिए एक पूर्ण प्रदर्शन और एएए अंतरिक्ष स्थिति बीबीबी के बाद छोड़ दिया है के अनुसार लपेटा गया है करना चाहते हैं? –
बिल्कुल ईईएस! – Stephane
मुझे लगता है कि मैं आपको गलत समझता हूं, आपका एएए एक दो पंक्ति टेक्स्ट व्यू है और बीबीबी के अनुसार अपनी दूसरी पंक्ति में लपेटा गया है, है ना? मुझे नहीं लगता कि अगर उत्तर हाँ है तो मैं मदद कर सकता हूं। –