以编程方式在线性布局中移动布局位置 [英] Moving layout position programmatically in linear layout
本文介绍了以编程方式在线性布局中移动布局位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_margin="10dip"
android:weightSum="1"
android:background="#000"
android:orientation="horizontal">
<TextView
android:id="@+id/textMessage"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight = ".9"
android:layout_gravity="center"
android:layout_margin="5dip"
android:paddingLeft="10dip"
android:background="#FFF"
android:text="TEXT NOT SET"
android:textColor="@android:color/white" />
<ImageView
android:id="@+id/thumbPhoto"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight = ".1"
android:src="@drawable/ic_contact_default"/>
</LinearLayout>
</LinearLayout>
我想做的是以编程方式将ImageView的位置更改为textMessage的左侧.
What i would like to do is to have the position of my ImageView change to the left side of my textMessage programmatically.
这可以通过将我的ImageView放在TextView上方来通过xml完成.但是,我希望通过编程实现相同的结果.
This could be done through xml by placing my ImageView above the TextView. However I would like to achieve the same result but programmatically.
假设我的适配器中有这个
Suppose I have this in my Adapter:
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;
}
推荐答案
尝试这样....
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);
它没有效果.因为在此处将重力应用于线性布局,所以此重力将其全部内容向左或向右对齐.
It shows no effect. Because here you are applying gravity to linearlayout, this gravity aligns its whole content either in left or right.
这篇关于以编程方式在线性布局中移动布局位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文