【问题标题】:Moving layout position programmatically in linear layout在线性布局中以编程方式移动布局位置
【发布时间】:2014-01-07 11:47:13
【问题描述】:

我有以下 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 的左侧。

这可以通过 xml 将我的 ImageView 放在 TextView 上方来完成。但是我想以编程方式实现相同的结果。

假设我的适配器中有这个:

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;
}

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    试试这样......

    ImageView imageView= (ImageView)yourLinearLayout.findViewById(R.id.thumbPhoto);
    yourLinearLayout.removeViewAt(1);
    yourLinearLayout.addView(imageView, 0);
    

    通过使用这条线

    // How should i change the position of my ImageView ??
            wrapper.setGravity([some_condition] ? Gravity.LEFT : Gravity.RIGHT);
    

    它没有显示任何效果。因为在这里您将重力应用到线性布局,所以这种重力会将其整个内容左对齐或右对齐。

    【讨论】:

    • 感谢您的建议,但我无法使用 addViewAt(int, view) 应用它,但我正在尝试使用 addView(int, view, some_other_param)
    • 是的,它应该是 yourLinearLayout.addView(view, index) 谢谢。我现在接受你的回答..
    • 任何方式..你明白了..享受。
    【解决方案2】:

    http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html

    有关如何为特定组件设置布局参数的更多详细信息,请参阅上述文档。

    LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
                        R.id.componentid;
    
    btn.leftMargin = x;
    btn.topMargin = y;
    btn.width = width;
    btn.height = height;
    
    imageview.setLayoutParams(layoutParam);
    

    以上代码将展示如何使用 layoutparam。

    【讨论】: