移动布局位置编程线性布局 [英] Moving layout position programatically in linear layout

查看:156
本文介绍了移动布局位置编程线性布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT>    <的LinearLayout
        机器人:ID =@ + ID /包装
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =50dp
        机器人:layout_margin =10dip
        机器人:weightSum =1
        机器人:背景=#000
        机器人:方向=横向>
        <的TextView
            机器人:ID =@ + ID /文字信息
            机器人:layout_width =0dp
            机器人:layout_height =FILL_PARENT
            机器人:layout_weight =9。
            机器人:layout_gravity =中心
            机器人:layout_margin =5dip
            机器人:paddingLeft =10dip
            机器人:背景=#FFF
            机器人:文字=TEXT NOT SET
            机器人:文字颜色=@机器人:彩色/白/>
        < ImageView的
            机器人:ID =@ + ID / thumbPhoto
            机器人:layout_width =0dp
            机器人:layout_height =FILL_PARENT
            机器人:layout_weight =.1
            机器人:SRC =@绘制/ ic_contact_default/>
    < / LinearLayout中>
< / LinearLayout中>

我想要做的是有我的ImageView变化的位置,我的文字信息编程的左侧。

这可以通过XML通过将我的ImageView TextView的上面来完成。但是我想达到同样的结果,但编程。

假设我在适配器有这种

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
      查看排= convertView;
        如果(行== NULL){
                LayoutInflater吹气=(LayoutInflater)this.getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
                行= inflater.inflate(R.layout.chat_item,父母,假);
        }        包装=(的LinearLayout)row.findViewById(R.id.wrapper);        //我应该如何改变我的ImageView的位置?
        wrapper.setGravity(?[some_condition] Gravity.LEFT:Gravity.RIGHT);        返回行;
}


解决方案

尝试这样的....

  ImageView的ImageView的=(ImageView的)yourLinearLayout.findViewById(R.id.thumbPhoto);
yourLinearLayout.removeViewAt(1);
yourLinearLayout.addViewAt(0,ImageView的);

通过使用此行

  //我应该如何改变我的ImageView的位置?
        wrapper.setGravity(?[some_condition] Gravity.LEFT:Gravity.RIGHT);

有没有显示出效果。因为在这里你申请重力的LinearLayout,这种重力对准无论是在左边或右边的全部内容。

Hi i have the following 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>

What i would like to do is to have the position of my ImageView change to the left side of my textMessage programmatically.

This could be done through xml by placing my ImageView above the TextView. However i would like to achieve the same result but programatically.

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

解决方案

try like this....

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

By using this line

// 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆