如何设置重力编程布局? [英] How to set gravity to layout programmatically?
问题描述
我创造,我需要调整accordingly.I需要对准那些从用户到右边,其他人左side.I聊天消息的聊天应用程序有一个列表视图中的消息displayed.For现在,我使用JSON捆绑获取data.Here是我的code
这是用于附加的字符串通过该检查该用户键入的信息和序分裂从用户识别的消息。
最后的EditText等=(EditText上)findViewById(R.id.EditText1);
最终按钮IMB =(按钮)findViewById(R.id.btn_send);
imb.setOnClickListener(新OnClickListener()
{
@覆盖
公共无效的onClick(查看arg0中)
{
字符串str = et.getText()的toString()+:AEIOU。
web.add(STR);
scrollMyListViewToBottom();
et.setText();
adapter.notifyDataSetChanged();
}
在这之后,我分割字符串,并存储在array.If的消息是来自user.ie,如果它有附加的字符串,则需要布局的重心设置为右,人离开。
我用这个code在我的适配器类此
mychat = web.get(位置);
如果(mychat.contains(:))
{
的String [] =分裂mychat.split(:);
字符串的聊天记录; 如果(分裂[1] .equalsIgnoreCase(AEIOU))
{ 聊天=分裂[0];
Toast.makeText(背景下,您输入....+聊天,Toast.LENGTH_SHORT).show();
的LinearLayout my_layout =(的LinearLayout)rowView.findViewById(R.id.main_layout); LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
my_layout.setLayoutParams(PARAMS);
//my_layout.setGravity(Gravity.RIGHT); txtTitle.setText(聊天);
}
}
其他
{
txtTitle.setText(web.get(位置));
}
txtTitle.setBackgroundResource(R.drawable.test);
什么是错在这?请帮助...
下面是我的XML
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / main_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#2a2a34
机器人:填充=3DP
机器人:方向=垂直> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
< RelativeLayout的
机器人:ID =@ + ID / chat_icon_layout
机器人:layout_width =match_parent
机器人:layout_weight =4
机器人:layout_height =match_parent> < ImageView的
机器人:ID =@ + ID / IMG
机器人:layout_width =60dp
机器人:layout_height =60dp
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真
机器人:SRC =@绘制/白/> < ImageView的
机器人:ID =@ + ID / IMG1
机器人:layout_width =55dp
机器人:layout_height =55dp
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真
机器人:SRC =@绘制/ abs__ab_bottom_solid_light_holo/> < / RelativeLayout的> <的LinearLayout
机器人:ID =@ + ID / chat_text_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:填充=0dp
机器人:layout_weight =1> <的TextView
机器人:ID =@ + ID / TXT
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=消息
机器人:textAppearance =机器人:ATTR / textAppearanceSmall
机器人:文字颜色=#FFFFFF/> < / LinearLayout中>
< / LinearLayout中>< / LinearLayout中>
由于要对齐聊天消息,左,右,因此,你需要调整你的短信即文本视图。因此,你需要做的。
如果(...)
txtTitle.setText(聊天);
txtTitle.setGravity(Gravity.RIGHT);
}
}
其他
{
txtTitle.setText(web.get(位置));
txtTitle.setGravity(Gravity.LEFT);
}
I am creating a chat application in which i need to align the chat messages accordingly.I need to align those from the user to right side and others to the left side.I have a listview in which the messages are displayed.For now,i am using bundled json for getting data.Here is my code
This is for appending a string to the message typed by the user which is checked and splitted inorder to recognize messages from the user.
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();
}
After that, i am splitting the string and stored in an array.If the message is from user.ie,if it has the appended string,then the gravity of the layout need to be set to RIGHT,else LEFT. I used this code in my adapter class for this
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);
What is wrong in this ?Please help...
Here is my 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>
since you want to align chat messages to left and right therefore you need to align your text messages i.e your text view. therefore you need to do
if(...)
txtTitle.setText(chats);
txtTitle.setGravity(Gravity.RIGHT);
}
}
else
{
txtTitle.setText(web.get(position));
txtTitle.setGravity(Gravity.LEFT);
}
这篇关于如何设置重力编程布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!