我想创建一个自定义布局。我用简单的适配器。当适配器创建TextView时,一切都可见。但是当我将LinearLayout与TextView一起用作孩子时,没有显示任何内容。我该如何解决这个问题?
package com.example.TApp.demo;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
/**
* Created by ChessMax on 18.07.2014.
*/
public class StudyGrid extends ViewGroup
{
private StudyGridAdapter mAdapter;
public StudyGrid(Context context) {
super(context);
}
public StudyGrid(Context context, AttributeSet attrs) {
super(context, attrs);
}
public StudyGrid(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
int itemWidth = 50;
int itemHeight = 50;
for(int i=0;i<mAdapter.getCount();i++){
View child = mAdapter.getView(i, null, this);
AdapterView.LayoutParams p = (AdapterView.LayoutParams) child.getLayoutParams();
if (p == null) {
p = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0);
}
addViewInLayout(child, i, p, true);
child.layout(itemWidth * i, 0, (i+1) * itemWidth, itemHeight);
}
this.invalidate();
}
public void setAdapter(StudyGridAdapter adapter)
{
mAdapter = adapter;
requestLayout();
}
}
package com.example.TApp.demo;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.TApp.R;
import java.util.List;
/**
* Created by ChessMax on 18.07.2014.
*/
public class StudyGridAdapter extends ArrayAdapter<StudyData>
{
private LayoutInflater mInflater;
public StudyGridAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
init(context);
}
public StudyGridAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
init(context);
}
public StudyGridAdapter(Context context, int textViewResourceId, StudyData[] objects) {
super(context, textViewResourceId, objects);
init(context);
}
public StudyGridAdapter(Context context, int resource, int textViewResourceId, StudyData[] objects) {
super(context, resource, textViewResourceId, objects);
init(context);
}
public StudyGridAdapter(Context context, int textViewResourceId, List<StudyData> objects) {
super(context, textViewResourceId, objects);
init(context);
}
public StudyGridAdapter(Context context, int resource, int textViewResourceId, List<StudyData> objects) {
super(context, resource, textViewResourceId, objects);
init(context);
}
private void init(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(position, convertView, parent);
}
private View createViewFromResource(int position, View convertView, ViewGroup parent) {
View view;
TextView text = null;
int resource = R.layout.study_cell;
if (convertView == null) {
view = mInflater.inflate(resource, parent, false);
} else {
view = convertView;
}
try {
if (view instanceof LinearLayout)
{
LinearLayout layout = (LinearLayout)view;
layout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
int count = layout.getChildCount();
for (int i = 0; i <= count; i++) {
View v = layout.getChildAt(i);
if (v instanceof TextView)
{
text = (TextView) v;
break;
}
}
}
else
{
text = (TextView) view;
}
} catch (ClassCastException e) {
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
throw new IllegalStateException(
"ArrayAdapter requires the resource ID to be a TextView", e);
}
if (text != null)
{
StudyData item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
} else {
text.setText(item.toString());
}
}
return view;
}
}
package com.example.TApp.demo;
/**
* Created by ChessMax on 18.07.2014.
*/
public class StudyData
{
private int num;
public StudyData(int num)
{
this.num = num;
}
@Override
public String toString()
{
return Integer.toString(num);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
package com.example.TApp;
import android.app.Activity;
import android.os.Bundle;
import com.example.TApp.demo.StudyData;
import com.example.TApp.demo.StudyGrid;
import com.example.TApp.demo.StudyGridAdapter;
import java.util.ArrayList;
/**
* Created by ChessMax on 18.07.2014.
*/
public class TestActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ArrayList<StudyData> list = new ArrayList<StudyData>();
list.add(new StudyData(5));
list.add(new StudyData(6));
list.add(new StudyData(7));
StudyGridAdapter adapter = new StudyGridAdapter(this, R.layout.study_cell, list);
StudyGrid grid = (StudyGrid) findViewById(R.id.studyGrid);
grid.setAdapter(adapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.TApp.demo.StudyGrid
android:id="@+id/studyGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
答案 0 :(得分:1)
检查线性布局方向。如果那是水平的那么你只能看到一个膨胀的视图
根视图linearlayout方向必须是垂直的。