更改 ListView 项目(行)的宽度?
Change width of a ListView item(row)?
我有一个 ListView
每行显示不同的百分比。
为此,我想更改其中一个内部布局宽度(内部膨胀 counterView
)。
例如:
如果 listview
中的第一个值项为 10%,则宽度也设置为 10px
如果第二个值项为 50%,则宽度也更改为 50px
像这样:
我该怎么做?
我尝试了这些代码,但它不起作用并且 width
没有改变:
(我在 XML
中为 layout
定义了 match_parent
宽度,我想以编程方式更改它)
public class MyAdapterScores extends BaseAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.scores_layout,null,true);
//I wanna change 'relativeLayoutRightSqure' width inside 'scores_layout'
View layout = convertView.findViewById(R.id.relativeLayoutRightSqure);
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) layout.getLayoutParams();
//variable precentage has value
p.width=precentage ;
layout.requestLayout();
你不能只将宽度设置为我认为的百分比。我还没有测试过这个解决方案,但试试吧!可能有用!
一个解决方案可能是将宽度设置为显示器总宽度的百分比,也许:
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int layoutWidth = size.x * (percentage / 100)
然后您可以使用该变量 "layoutWidth" 来设置您想要的布局宽度。
请使用以下代码
RelativeLayout layout = convertView.findViewById(R.id.relativeLayoutRightSqure);
RelativeLayout.LayoutParams param=layout.getLayoutParams();
param.width=precentage ;
layout.setLayoutParams(param);
我找到问题了!
问题是同时为我的内部 counterView
布局视图定义 android:layout_toRightOf
和 android:layout_toLeftOf
,导致设置宽度不起作用!!
(
参考上面的代码(relativeLayoutRightSqure
):
View layout = convertView.findViewById(R.id.relativeLayoutRightSqure);
)
我有一个 ListView
每行显示不同的百分比。
为此,我想更改其中一个内部布局宽度(内部膨胀 counterView
)。
例如:
如果 listview
中的第一个值项为 10%,则宽度也设置为 10px
如果第二个值项为 50%,则宽度也更改为 50px
像这样:
我该怎么做?
我尝试了这些代码,但它不起作用并且 width
没有改变:
(我在 XML
中为 layout
定义了 match_parent
宽度,我想以编程方式更改它)
public class MyAdapterScores extends BaseAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.scores_layout,null,true);
//I wanna change 'relativeLayoutRightSqure' width inside 'scores_layout'
View layout = convertView.findViewById(R.id.relativeLayoutRightSqure);
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) layout.getLayoutParams();
//variable precentage has value
p.width=precentage ;
layout.requestLayout();
你不能只将宽度设置为我认为的百分比。我还没有测试过这个解决方案,但试试吧!可能有用!
一个解决方案可能是将宽度设置为显示器总宽度的百分比,也许:
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int layoutWidth = size.x * (percentage / 100)
然后您可以使用该变量 "layoutWidth" 来设置您想要的布局宽度。
请使用以下代码
RelativeLayout layout = convertView.findViewById(R.id.relativeLayoutRightSqure);
RelativeLayout.LayoutParams param=layout.getLayoutParams();
param.width=precentage ;
layout.setLayoutParams(param);
我找到问题了!
问题是同时为我的内部 counterView
布局视图定义 android:layout_toRightOf
和 android:layout_toLeftOf
,导致设置宽度不起作用!!
(
参考上面的代码(relativeLayoutRightSqure
):
View layout = convertView.findViewById(R.id.relativeLayoutRightSqure);
)