Android 如何放大子视图?

Android 如何放大子视图?,android,layout,layout-inflater,Android,Layout,Layout Inflater,我试图膨胀一个布局的特定部分,但是我得到了编译器错误,并且不确定采取什么方法。特别是在以下方面: LinearLayout childList = (LinearLayout)findViewById(R.id.listLayout). //(This can be found near the bottom of Java code.) 我还不确定在尝试膨胀列表布局时是否采取了正确的方法 在这个阶段,我试图在顶部有一个按钮,然后在它下面的数组中动态地膨胀一个项目列表 任何帮助都将不胜感激

我试图膨胀一个布局的特定部分,但是我得到了编译器错误,并且不确定采取什么方法。特别是在以下方面:

LinearLayout childList = (LinearLayout)findViewById(R.id.listLayout). 
//(This can be found near the bottom of Java code.)
我还不确定在尝试膨胀
列表布局时是否采取了正确的方法

在这个阶段,我试图在顶部有一个按钮,然后在它下面的数组中动态地膨胀一个项目列表

任何帮助都将不胜感激

XML

<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="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/labelAddCourseButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="addCourseButton"
        android:padding="10dp"
        android:text="@string/CourseName" />

    <LinearLayout
    android:id="@+id/listLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >     

    <TextView
        android:id="@+id/labelModuleCode"
        android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:layout_marginLeft="10dp"
        android:text="@string/CourseName"
        android:textSize="10dp" />

     <TextView
        android:id="@+id/labelCourseType"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/CourseType"
        android:layout_marginLeft="10dp"
        android:textSize="10dp" />

    </LinearLayout>

</LinearLayout>

Java

package com.example.mycoursetimetable;

import android.os.Bundle;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MyCourses extends ListActivity {

    static final String TEST = "com.example.mycoursetimetable.TEST";
    String [] MODULE;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_courses);
        MODULE = getResources().getStringArray(R.array.module);
        setListAdapter(new ListArrayAdapter(this,MODULE));
    }

    public void addCourseButton (View addCourseButton) 
    {
        Intent intent = new Intent(this,AddCourse.class);
        startActivity(intent);
    }

    public void onListItemClick(ListView l, View v, int position, long id)
    {
        super.onListItemClick(l, v, position, id);

        try {
        Class test = Class.forName("com.example.MyCourseTimeTable.AddCourse");
        Intent intent = new Intent(MyCourses.this, test);

        TextView textView = (TextView) v.findViewById(R.id.labelModuleCode);
        String module = textView.getText().toString();

        intent.putExtra(TEST,module);
        startActivity(intent);
        }   
        catch (ClassNotFoundException e)
        {
        e.printStackTrace();
        }             
    }    
}

class ListArrayAdapter extends ArrayAdapter<String> 
{
    //Context allows the retrieval of resources such as layout
    private final Context context;
    private final String[] test;

    //create the ArrayAdpater
    public ListArrayAdapter(Context context, String[] test) 
    {
        super(context, R.layout.activity_my_courses, test);
        this.context = context;
        this.test = test;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //inflater  dynamically loads the layout

        LinearLayout childList = (LinearLayout)findViewById(R.id.listLayout);
        View rowView = inflater.inflate(R.layout.childList, parent, false);

        //get the textView
        TextView textView = (TextView) rowView.findViewById(R.id.labelModuleCode);
        //set the text to the string values based on position
        textView.setText(test[position]);

        // Change item based on its position in the string array
        String modulePosition = test[position];             

        //return the layout
        return rowView;
    }
}
package com.example.mycourse;
导入android.os.Bundle;
导入android.app.ListActivity;
导入android.content.Context;
导入android.content.Intent;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.LinearLayout;
导入android.widget.ListView;
导入android.widget.TextView;
公共类MyCourses扩展了ListActivity{
静态最终字符串TEST=“com.example.myCourseThedule.TEST”;
字符串[]模块;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u my\u课程);
MODULE=getResources().getStringArray(R.array.MODULE);
setListAdapter(新ListArrayAdapter(此模块));
}
公共无效addCourseButton(查看addCourseButton)
{
意向意向=新意向(此,AddCourse.class);
星触觉(意向);
}
public void onListItemClick(列表视图l、视图v、整数位置、长id)
{
super.onListItemClick(左、右、位置、id);
试一试{
Class test=Class.forName(“com.example.myCourseThedule.AddCourse”);
意向意向=新意向(MyCourses.this,test);
TextView TextView=(TextView)v.findViewById(R.id.labelModuleCode);
字符串模块=textView.getText().toString();
意图.额外(测试、模块);
星触觉(意向);
}   
catch(classnotfounde异常)
{
e、 printStackTrace();
}             
}    
}
类ListArrayAdapter扩展了ArrayAdapter
{
//上下文允许检索布局等资源
私人最终语境;
私有最终字符串[]测试;
//创建ArrayAdpater
公共ListArrayAdapter(上下文,字符串[]测试)
{
超级(上下文、R.layout.activity\u my\u课程、测试);
this.context=上下文;
这个。测试=测试;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
//充气器动态加载布局
LinearLayout childList=(LinearLayout)findViewById(R.id.listLayout);
视图行视图=充气机。充气(R.layout.childList,父,false);
//获取文本视图
TextView TextView=(TextView)rowView.findViewById(R.id.labelModuleCode);
//根据位置将文本设置为字符串值
setText(测试[位置]);
//根据项目在字符串数组中的位置更改项目
字符串模块位置=测试[位置];
//返回布局
返回行视图;
}
}

尝试将
getView()
编辑为以下内容:

       @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      //inflater  dynamically loads the layout

        LinearLayout childList = (LinearLayout)findViewById(R.id.listLayout);
        convertView = inflater.inflate(R.layout.childList, null);

        //get the textView
        TextView textView = (TextView) convertView.findViewById(R.id.labelModuleCode);
        //set the text to the string values based on position
        textView.setText(test[position]);

        // Change item based on its position in the string array
        String modulePosition = test[position];



        //return the layout
        return convertView;
        }

使用
getView()
convertView
视图,而不是您自己的
rowView

以下是您可能需要的示例。通过观看Android的Romain Guy,了解我为什么使用ViewHolder和其他技巧:

类ListArrayAdapter扩展了ArrayAdapter
{
//您只需要一份LayoutInflater
私人充气机;
//创建ArrayAdpater
公共ListArrayAdapter(上下文,字符串[]测试)
{
//这里的布局并不重要,因为我们不使用它
super(上下文、R.layout.childList、测试);
充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
视窗座;
if(convertView==null){
//为每行充气所需的布局
convertView=充气机。充气(R.layout.childList,父项,false);
holder=新的ViewHolder();
holder.code=(TextView)convertView.findViewById(R.id.labelModuleCode);
holder.type=(TextView)convertView.findViewById(R.id.labelModuleType);
//在ViewHolder中为你的按钮添加一个引用,找到它就像TextView一样,并在这里定义它的OnClickListener,最终。。。
convertView.setTag(支架);
}
其他的
holder=(ViewHolder)convertView.getTag();
字符串模块=getItem(位置);
//根据位置将文本设置为字符串值
holder.code.setText(模块);
//返回布局
返回视图;
}
类视图持有者{
文本视图代码;
文本视图类型;
}
}

你能分享一下编译器的错误吗?我可以看出你对膨胀布局的想法有点困惑,但你已经走得不远了。然而,发布布局
activity\u my_courses.xml
childList.xml
,这样我就可以看到你想要做什么。发布虽然现在我认为我对子布局的定义完全不正确,谢谢,我尝试了你的建议,但eclipse仍然强调findViewById是一个错误。是因为它在ListArrayAdapter类中,如果是,我该如何解决它?在我的项目中,我不使用ListActivty,而是使用Activity,并使用BaseAdapter而不是ArrayAdapter.Perfect。非常感谢你。非常感谢。
class ListArrayAdapter extends ArrayAdapter<String> 
{
    //  You only need one copy of LayoutInflater
    private final LayoutInflater inflater;

    //create the ArrayAdpater
    public ListArrayAdapter(Context context, String[] test) 
    {
        // The layout here doesn't really matter since we don't use it
        super(context, R.layout.childList, test);
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewHolder holder;
        if(convertView == null) {
            // Inflate the layout that you want for each row
            convertView = inflater.inflate(R.layout.childList, parent, false);

            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.labelModuleCode);
            holder.type = (TextView) convertView.findViewById(R.id.labelModuleType);

            // Add a reference in ViewHolder for your Button, find it like the TextViews and define its OnClickListener here too, eventually...

            convertView.setTag(holder);
        }
        else 
            holder = (ViewHolder) convertView.getTag();

        String module = getItem(position);
        //set the text to the string values based on position
        holder.code.setText(module);

        //return the layout
        return convertView;
    }

    class ViewHolder {
        TextView code;
        TextView type;
    }
}