2017-02-05 16 views
-4

作成する必要がありますTextView左揃えと右揃え同じ行にテキストを配置し、このように出力します。私はHTMLテキストか何か財産左のテキストと右のテキスト、方法のようないくつかのものを使用して解決策を探してることはsetCompoundDrawablesAndroid TextViewの左揃えと右揃え同じ行のテキストの整列

enter image description here

これは出力

enter image description here

電流であり、これはあります現在のコード

PackageInfo packageInfo = (PackageInfo) getItem(position); 
Drawable appIcon = packageManager.getApplicationIcon(packageInfo.applicationInfo); 
int inPixels= (int) context.getResources().getDimension(R.dimen.iconsize); 
appIcon.setBounds(0, 0, inPixels, inPixels); 
holder.apkName.setCompoundDrawables(appIcon, null, null, null); 
holder.apkName.setCompoundDrawablePadding(15); 

String appName = packageManager.getApplicationLabel(packageInfo.applicationInfo).toString(); 
Date date=new Date(packageInfo.firstInstallTime); 
SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy"); 
String dateText = df2.format(date); 
holder.apkName.setText(Html.fromHtml("<b>"+appName+"</b>("+dateText+")")); 
+0

あなたは手の込んだていただけますか? –

+1

Android TextViewの左揃えと右揃え同じ行のテキストの整列 –

+0

ああ、テキストは左隅と右隅に表示しますか? –

答えて

1

あなたが別のTextViewを作成しないようにしようとしている場合、あなたはSpannableStringを使用する必要があります。

私が思うに、この溶液をhere

を実証しているあなたは、これがSpannableStringを使用した後にどのように見えるかですthis link

+0

final String resultText = LeftText + "" + RightText; final SpannableString styledResultText =新しいSpannableString(resultText); styledResultText.setSpan((新しいAlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE)、LeftText.length()+ 2、LeftText.length()+ 2 + RightText.length()、Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); resultTextView.setText(styledResultText) ; –

+1

は何を言っても動作しません –

+0

投稿にいくつかのバグなどがありますが、SpannableStringのアイデアがありました。ありがとう! –

0

2 textViewsを使用すると、あなたの結果得られます: をここに例を示します

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weight="1" 
     android:gravity="left" 
     android:text="Text on the Left" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weight="1" 
     android:gravity="right" 
     android:text="Text on the Right" /> 
</LinearLayout> 
+1

@Hey Krunal質問ありがとうございました。 weightSumを使用している場合、子供のためにlayuot_widthを設定する必要はありません。単純に0dpにするか、weightSumを設定しないでください。 –

+0

@UmeshSonawane私は試してみました。 目的の出力を得る方法はたくさんあります。 –

+2

私はそれが動作しないと言っているのではない私はちょうどあなたがレイアウトの幅が設定されている場合あなたに言っておきたいweighSumの使用はありません –

0

を通じてより多くのアイデアを得る可能性があります。そして、これは私が欲しいものです。ありがとうございました!

enter image description here

そして、最終的なコードは次のとおりです。

PackageInfo packageInfo = (PackageInfo) getItem(position); 
Drawable appIcon = packageManager.getApplicationIcon(packageInfo.applicationInfo); 
int inPixels= (int) context.getResources().getDimension(R.dimen.iconsize); 
appIcon.setBounds(0, 0, inPixels, inPixels); 
holder.apkName.setCompoundDrawables(appIcon, null, null, null); 
holder.apkName.setCompoundDrawablePadding(15); 

String appName = packageManager.getApplicationLabel(packageInfo.applicationInfo).toString(); 
Date date=new Date(packageInfo.firstInstallTime); 
SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy"); 
String dateText = df2.format(date); 
String LeftText=appName; 
String RightText=dateText; 
final String resultText = LeftText + "\n" + RightText; 
final SpannableString styledResultText = new SpannableString(resultText); 
styledResultText.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE), LeftText.length() , LeftText.length() +RightText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
holder.apkName.setText(styledResultText); 
関連する問題