我有以下xml:
我想做的是让我的ImageView的位置以编程方式更改到textMessage的左侧.
这可以通过将我的ImageView放在TextView上方通过xml完成.但是我想以编程方式实现相同的结果.
假设我的适配器中有这个:
public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; if (row == null) { LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = inflater.inflate(R.layout.chat_item, parent, false); } wrapper = (LinearLayout) row.findViewById(R.id.wrapper); // How should I change the position of my ImageView ?? wrapper.setGravity([some_condition] ? Gravity.LEFT : Gravity.RIGHT); return row; }
saa.. 10
试试这样....
ImageView imageView= (ImageView)yourLinearLayout.findViewById(R.id.thumbPhoto); yourLinearLayout.removeViewAt(1); yourLinearLayout.addView(0, imageView);
通过使用此行
// How should i change the position of my ImageView ?? wrapper.setGravity([some_condition] ? Gravity.LEFT : Gravity.RIGHT);
它显示没有效果.因为在这里你将重力应用于线性布局,这种重力将其整个内容左右对齐.
试试这样....
ImageView imageView= (ImageView)yourLinearLayout.findViewById(R.id.thumbPhoto); yourLinearLayout.removeViewAt(1); yourLinearLayout.addView(0, imageView);
通过使用此行
// How should i change the position of my ImageView ?? wrapper.setGravity([some_condition] ? Gravity.LEFT : Gravity.RIGHT);
它显示没有效果.因为在这里你将重力应用于线性布局,这种重力将其整个内容左右对齐.