【发布时间】:2014-02-18 03:09:28
【问题描述】:
我希望列表项的背景像进度条一样工作。 例如,在 tTorrent 文件列表中:
现在是这样完成的:我使用一个相对布局和两个线性布局。一个有文本视图,另一个有两个用作进度条的视图。 “进度”使用视图的权重进行更改,该权重在 getView 方法中动态设置。然后将带有 textviews 的线性布局放在前面。代码如下:
布局:
<LinearLayout
android:padding="5dp"
android:id="@+id/catStatsRowLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- textviews on the front -->
...
</LinearLayout>
<!-- views which work as progress bar -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@id/catStatsRowLay"
android:layout_alignLeft="@id/catStatsRowLay"
android:layout_alignRight="@id/catStatsRowLay"
android:layout_alignTop="@id/catStatsRowLay"
android:layout_below="@id/catStatsRowLay"
android:layoutDirection="rtl"
android:orientation="horizontal" >
<View
android:id="@+id/catStatsRowBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".70"
android:background="#cc550000" />
<View
android:id="@+id/catStatsRowBar2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".30"
android:background="@android:color/transparent" />
</LinearLayout>
</RelativeLayout>
还有getView方法:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.categories_stats_list_row,
parent, false);
//filling textviews
...
//getting bars that work as progressbar
View bar = (View) rowView.findViewById(R.id.catStatsRowBar);
View bar2 = (View) rowView.findViewById(R.id.catStatsRowBar2);
//calculating and setting weights
float percent = cs.getBarWeight();
LinearLayout.LayoutParams barPar = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, (1-percent));
LinearLayout.LayoutParams bar2Par = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, percent);
bar.setLayoutParams(barPar);
bar2.setLayoutParams(bar2Par);
bar.setBackgroundColor(cs.getColor(position));
//bringing LinearLayout with textviews to front
LinearLayout ll = (LinearLayout) rowView
.findViewById(R.id.catStatsRowLay);
ll.bringToFront();
return rowView;
}
它在 4.2.2 及更低版本的设备上完美运行(截图如下)。
问题:4.3 及更高版本(模拟器和设备)不显示“进度”视图。我尝试不将带有文本视图的线性布局放在前面,将权重设置为常量,更改线性布局的对齐方式 - 没有结果,“进度”视图未显示。如何让它在新版本的 android 上运行?
【问题讨论】:
-
你能在这里发布一个完整的适配器类吗?我正在尝试实现类似的程序。这会有很大帮助。
-
@AmaanMemon 抱歉,已经 3.5 年了,来源已经丢失。如果您有任何疑问,我可以提供帮助。
-
我喜欢你的想法 :) 。在权重方面取得进展是开箱即用的解决方案:D
标签: android android-layout android-listview android-progressbar