如何以编程方式将重力设置为布局?
我正在创建一个聊天应用程序,其中我需要相应地对齐聊天消息。我需要将这些来自用户的消息与右侧对齐,并将其他消息与左侧对齐。我有一个列表视图,其中显示了消息。现在,我我正在使用捆绑的json来获取数据。这里是我的代码
这是为了将一个字符串附加到由用户输入的消息中,该消息被检查并拆分以识别来自用户的消息。
final EditText et = (EditText)findViewById(R.id.EditText1);
final Button imb=(Button)findViewById(R.id.btn_send);
imb.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
String str = et.getText().toString()+":aeiou";
web.add(str);
scrollMyListViewToBottom();
et.setText(" ");
adapter.notifyDataSetChanged();
}
之后,我分割字符串并存储在数组中。如果消息来自user.ie,如果它具有附加字符串,则布局的重力需要设置为RIGHT,否则为LEFT。 我在我的适配器类中为此使用了此代码
mychat=web.get(position);
if(mychat.contains(":"))
{
String[] splitted = mychat.split(":");
String chats;
if(splitted[1].equalsIgnoreCase("aeiou"))
{
chats=splitted[0];
Toast.makeText(context, "You entered...."+chats, Toast.LENGTH_SHORT).show();
LinearLayout my_layout = (LinearLayout) rowView.findViewById(R.id.main_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
my_layout.setLayoutParams(params);
//my_layout.setGravity(Gravity.RIGHT);
txtTitle.setText(chats);
}
}
else
{
txtTitle.setText(web.get(position));
}
txtTitle.setBackgroundResource(R.drawable.test);
这有什么不对?请帮忙...
这是我的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2a2a34"
android:padding="3dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/chat_icon_layout"
android:layout_width="match_parent"
android:layout_weight="4"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/img"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/white" />
<ImageView
android:id="@+id/img1"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/abs__ab_bottom_solid_light_holo" />
</RelativeLayout>
<LinearLayout
android:id="@+id/chat_text_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:layout_weight="1" >
<TextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Message"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
因为您想要将聊天消息左右对齐,所以您需要对齐文本消息,即文本视图。 因此你需要这样做
if(...)
txtTitle.setText(chats);
txtTitle.setGravity(Gravity.RIGHT);
}
}
else
{
txtTitle.setText(web.get(position));
txtTitle.setGravity(Gravity.LEFT);
}
在你的适配器中,你应该将其他情况设置为其他情况。 示例如下。
我看到你正在为你的宽度使用wrap_content。 因此,您的LinearLayout将包装您的内容,并且无法对齐到布局的左侧或右侧。 如果有足够的空间,您应该为您的LinearLayout使用填充父项,以便对齐它的内容
if(splitted[1].equalsIgnoreCase("aeiou"))
{
chats=splitted[0];
Toast.makeText(context, "You entered...."+chats, Toast.LENGTH_SHORT).show();
LinearLayout my_layout = (LinearLayout) rowView.findViewById(R.id.main_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
my_layout.setLayoutParams(params);
//my_layout.setGravity(Gravity.RIGHT);
txtTitle.setText(chats);
}
}
else{
LinearLayout my_layout = (LinearLayout)rowView.findViewById(R.id.main_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.LEFT;
my_layout.setLayoutParams(params);
txtTitle.setText(web.get(position));
}
问题是你正在设置布局的权重..尝试设置textview重力左或右..
只需在xml android:gravity =“left”或android:gravity =“right”中添加属性即可。
链接地址: http://www.djcxy.com/p/93271.html