为什么做出来的界面如此的丑?

YXTS122 2016-09-12 08:58:39
MainActivity.java
package com.example.ch02_qipao;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
private ListView msgListView;
private EditText inputText;
private Button send;
private MsgAdapter adapter;
private List<Msg> msgList=new ArrayList<Msg>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initMsgs();
adapter=new MsgAdapter(MainActivity.this,R.layout.msg_item,msgList);
inputText=(EditText)findViewById(R.id.input_text);
send=(Button)findViewById(R.id.send);
msgListView=(ListView)findViewById(R.id.msg_list_view);
msgListView.setAdapter(adapter);
send.setOnClickListener(this);
}

private void initMsgs() {
Msg msg1=new Msg("大神,快醒醒,你还有一大串代码没帮我改呢!",Msg.TYPE_RECEIVED);
msgList.add(msg1);
Msg msg2=new Msg("我快不行了,你找别人吧!",Msg.TYPE_SENT);
msgList.add(msg2);
}

@Override
public void onClick(View v) {
String content=inputText.getText().toString().trim();
if(!"".equals(content)) {
Msg msg=new Msg(content,Msg.TYPE_SENT);
msgList.add(msg);
adapter.notifyDataSetChanged();
msgListView.setSelection(msgList.size());
inputText.setText("");
}
else {
Toast toast1=Toast.makeText(getApplicationContext(),"不能发送空内容!",Toast.LENGTH_SHORT);
toast1.show();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


Msg.java
package com.example.ch02_qipao;

public class Msg {
public static final int TYPE_RECEIVED=0;
public static final int TYPE_SENT=1;
private String content;
private int type;

public Msg(String content,int type)
{
this.content=content;
this.type=type;
}

public String getContent()
{
return content;
}

public int getType()
{
return type;
}

}


MsgAdapter.java
package com.example.ch02_qipao;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MsgAdapter extends ArrayAdapter<Msg>{
private int resourceId;
public MsgAdapter(Context context,int textViewResourceId,List<Msg> objects) {
super(context,textViewResourceId,objects);
resourceId=textViewResourceId;
}

@Override
public View getView(int position,View convertView,ViewGroup parent) {
Msg msg=getItem(position);
View view;
ViewHolder viewHolder;
if(convertView==null) {
view=LayoutInflater.from(getContext()).inflate(resourceId,null);
viewHolder=new ViewHolder();
viewHolder.leftLayout=(LinearLayout)view.findViewById(R.id.left_layout);
viewHolder.rightLayout=(LinearLayout)view.findViewById(R.id.right_layout);
viewHolder.leftMsg=(TextView)view.findViewById(R.id.left_msg);
viewHolder.rightMsg=(TextView)view.findViewById(R.id.right_msg);
view.setTag(viewHolder);
}
else {
view=convertView;
viewHolder=(ViewHolder)view.getTag();
}
if(msg.getType()==Msg.TYPE_RECEIVED) {
viewHolder.leftLayout.setVisibility(View.VISIBLE);
viewHolder.rightLayout.setVisibility(View.GONE);
viewHolder.leftMsg.setText(msg.getContent());
}
else if(msg.getType()==Msg.TYPE_SENT) {
viewHolder.rightLayout.setVisibility(View.VISIBLE);
viewHolder.leftLayout.setVisibility(View.GONE);
viewHolder.rightMsg.setText(msg.getContent());
}
return view;
}

class ViewHolder {
LinearLayout leftLayout;
LinearLayout rightLayout;
TextView leftMsg;
TextView rightMsg;
}

}


activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ch02_qipao.MainActivity"
android:background="#d8e0e8"
android:orientation="vertical">

<include
layout="@layout/title" />

<ListView
android:id="@+id/msg_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#0000">

</ListView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/input_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/something"
android:maxLines="3"/>

<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send"/>

</LinearLayout>

</LinearLayout>


msg_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ch02_qipao.MainActivity"
android:background="#d8e0e8"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout
android:id="@+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:background="@drawable/message_left">

<TextView
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff"/>
</LinearLayout>


<LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/message_right">

<TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff"/>
</LinearLayout>

</LinearLayout>


title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ch02_qipao.MainActivity"
android:orientation="horizontal">

<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/fanhui"
android:textSize="15sp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/liao"
android:textSize="15sp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/xinjian"
android:textSize="15sp"/>

</LinearLayout>


strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">气泡</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="something">输入信息</string>
<string name="send">发送</string>
<string name="fanhui">返回</string>
<string name="liao">与好友聊天</string>
<string name="xinjian">新建</string>

</resources>





问题:
1.文字怎么不能放在气泡里?
2.返回,与好友聊天,新建三个控件的顶部怎么和父部件对齐?用了layout_alignParentTop,它报错:
Invalid layout param in a linearlayout:layout_alignParentTop
...全文
363 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXTS122 2016-09-15
  • 打赏
  • 举报
回复
android:background="#ffc0cb">
      
YXTS122 2016-09-14
  • 打赏
  • 举报
回复
title.xml
<TextView 
android:layout_width="0dp"
android:layout_weight="2"
android:gravity="center"

使用了layout_weight,layout_width就不起作用,所以为0dp
运行如下:
幻影宇寰 2016-09-12
  • 打赏
  • 举报
回复
布局的方式不对,气泡需要使用.9图,这样只要使用图片作为背景就行,其他布局可以参考特殊的listview就行,网上有源码,我这里提供个包含很多开源项目的地址给你,希望对你有所帮助。地址:https://github.com/xiaoyaoyou1212/android-open-project
  • 打赏
  • 举报
回复
用了layout_alignParentTop,它报错: Invalid layout param in a linearlayout:layout_alignParentTop 你布局用的LinearLayout,这个属性用在RelativeLayout 图片问题的话,可能是你的图片四周本身就有空出来的地方,所以给textview设置背景的时候就看起来文字在图片外面

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧