如何避免在android中的relativelayout中重叠视图
Posted
技术标签:
【中文标题】如何避免在android中的relativelayout中重叠视图【英文标题】:how to avoid overlapping views inside relativelayout in android 【发布时间】:2014-09-18 06:03:12 【问题描述】:我在 android 中动态生成视图时遇到了这个问题。 我有相对布局,在里面我正在生成动态视图。但这仅重叠第一个和第二个视图。我搜索并尝试了更多。任何人都可以帮我解决这个问题。谢谢。这是我尝试过的代码。 动态数据适配器.java 包 com.Nithya.dynamiclist;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
public class Dynamicdataadapter extends BaseAdapter
Context context;
ArrayList<String> data;
int layoutid;
RelativeLayout itemlayout;
ArrayList<Integer> textviewsize = new ArrayList<Integer>();
ArrayList<Integer> buttonsize = new ArrayList<Integer>();
public Dynamicdataadapter(Context context,ArrayList<String> datas, int childlayout)
this.context = context;
this.data = datas;
this.layoutid = childlayout;
textviewsize.add(2);
textviewsize.add(5);
textviewsize.add(3);
@Override
public int getCount()
return data.size();
@Override
public Object getItem(int position)
return position;
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
View row = convertView ;
Object[] textObjects = textviewsize.toArray();
if(row == null)
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutid, parent, false);
itemlayout = (RelativeLayout)row.findViewById(R.id.itemlayout);
for(int room = 0 ; room < data.size() ; room++)
if(position == room)
TextView[] textview = new TextView[(Integer)textObjects[room]];
int previousid = 0;
int currentid = 0;
RelativeLayout rl = new RelativeLayout(context);
rl.setId(123456);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rl.setLayoutParams(lp);
for(int i =0 ; i < textview.length;i++)
currentid = previousid + 1;
textview[i] = new TextView(context);
textview[i].setId(i);
textview[i].setText("text"+i);
RelativeLayout.LayoutParams lp_btn1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp_btn1.addRule(RelativeLayout.RIGHT_OF,(textview[i].getId()-1));
/*if(i > 0)
lp_btn1.addRule(RelativeLayout.RIGHT_OF,previousid);
*/
textview[i].setLayoutParams(lp_btn1);
//textview[i].setPadding(0, 0, 10, 0);
rl.addView(textview[i]);
previousid = currentid;
itemlayout.addView(rl);
return row;
MainActivity.java
package com.Nithya.dynamiclist;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity
ListView dynamiclistview;
ArrayList<String> datas;
ArrayList<String> datalist;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dynamiclistview = (ListView)findViewById(R.id.dynamiclistview);
datas = new ArrayList<String>();
datalist = new ArrayList<String>();
datas.add("abc");
datas.add("efg");
datas.add("hij");
Dynamicdataadapter dynamicadapter = new Dynamicdataadapter(this,datas,R.layout.childlayout);
dynamiclistview.setAdapter(dynamicadapter);
readdatafromassests();
public void readdatafromassests()
AssetManager assetmanager = getAssets();
try
BufferedReader bufferedreader = new BufferedReader(
new InputStreamReader(assetmanager.open("datasheet")));
String readline ;
StringBuilder builder = new StringBuilder();
while((readline = bufferedreader.readLine()) != null)
Log.d("datas", ""+readline);
if(readline.contains("Textview"))
Log.d("datasheet datas", ""+readline);
builder.append(readline);
String datasheet = builder.toString();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
@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);
【问题讨论】:
【参考方案1】:使用 LinerLayout 代替 RelaytiveLayout
@Override
public View getView(int position, View convertView, ViewGroup parent)
View row = convertView ;
Object[] textObjects = textviewsize.toArray();
if(row == null)
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutid, parent, false);
itemlayout = (LinearLayout)row.findViewById(R.id.itemlayout);
for(int room = 0 ; room < data.size() ; room++)
if(position == room)
TextView[] textview = new TextView[(Integer)textObjects[room]];
int previousid = 0;
int currentid = 0;
LinearLayout rl = new LinearLayout(context);
rl.setId(123456);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp.setLayoutDirection(LinearLayout.HORIZONTAL);
rl.setLayoutParams(lp);
for(int i =0 ; i < textview.length;i++)
currentid = previousid + 1;
textview[i] = new TextView(context);
textview[i].setId(i);
textview[i].setText("text"+i);
LinearLayout.LayoutParams lp_btn1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp_btn1.setLayoutDirection(LinearLayout.HORIZONTAL);
//lp_btn1.addRule(RelativeLayout.RIGHT_OF,(textview[i].getId()-1));
/*if(i > 0)
lp_btn1.addRule(RelativeLayout.RIGHT_OF,previousid);
*/
textview[i].setLayoutParams(lp_btn1);
//textview[i].setPadding(0, 0, 10, 0);
rl.addView(textview[i]);
previousid = currentid;
itemlayout.addView(rl);
return row;
【讨论】:
【参考方案2】:不确定是否是问题所在,但您在这里使用了否定的 id:
// first iteration i == 0
// setId(0)
textview[i].setId(i);
// RelativeLayout.RIGHT_OF, -1
lp_btn1.addRule(RelativeLayout.RIGHT_OF,(textview[i].getId()-1));
id为-1的TextView不存在。
【讨论】:
以上是关于如何避免在android中的relativelayout中重叠视图的主要内容,如果未能解决你的问题,请参考以下文章
OnCreate 中的 AsyncTask 在运行时失败,如何在 Android 中避免这种情况?
android - 过度绘制布局允许通过 LinearLayout 进行触摸
如果 Android 中的 HttpGet 操作持续时间过长,如何避免出现错误 10053 (WSAECONNABORTED)?