我需要在同一行创建TextView
左对齐和右对齐文本,并输出如下。我正在使用HTML文本或其他属性Left Text和Right Text搜索解决方案,有点像方法setCompoundDrawables
这是当前的输出
这是当前代码
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)
答案 1 :(得分:0)
使用两个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>
答案 2 :(得分:0)
这是使用SpannableString的方式。这就是我想要的。谢谢!
最终代码是:
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);