相对布局问题

发布于 2024-11-27 01:48:25 字数 5069 浏览 1 评论 0原文

我实现了一个系统,使图像按钮等于可绘制对象(餐厅徽标)的数量,因此我过滤了以前缀“rest”开头的图像。到这里还可以。然后我创建一个 GridWiev 并将按钮放入其中,根布局是一个relativelayout。然而,编译后它不显示任何图像按钮。有我的代码和xml。 感谢您的任何帮助。

public class RestListActivity extends Activity{
    ImageButton [] buttons;
    RelativeLayout rl;
    int restCount=0;
    Resources a;
    int resIndex=0;
    boolean resFound=false;
    int last;
    int resCount=0;
    GridView gv;
    // TODO Auto-generated constructor stub
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rl = (RelativeLayout) findViewById(R.id.relativeLayoutRest);
        if(rl==null)
            Log.i("***********","RLOUT NULL AGA");
        else
            Log.i("***********","RLOUT VAR AGA");
        setContentView(R.layout.restlist);
        Log.i("asd","asd");

        gv=new GridView(this);
        a=this.getResources();
        int i=0x7f020000;
        boolean done=true;
        if(rl==null)
            Log.i("***********","RLOUT NULL AGA");
        else
            Log.i("***********","RLOUT VAR AGA");
        Log.i("asd","asd");
        try
        {
            Log.i("rest buttons","______");
            for (i=0x7f020000 ;done&&i<0x7f020040;i++)
            {
                Log.i("rest buttons","----");
                String name=a.getResourceName(i);
                name=name.substring(name.indexOf(':')+1);
                name=name.substring(9);
                if(name.startsWith("rest"))
                {
                    if(!resFound)
                    {
                        resIndex=i;
                        resFound=true;
                    }
                    Log.i("rest buttons",name);

                    resCount++;
                }
            }
        }
        catch(Exception e)
        {
            last=i;
            Log.i("LAST",i+"");
            done=false;
            e.printStackTrace();
        }
        buttons=new ImageButton[resCount];
        Button orderButton = (Button)findViewById(R.id.adbutton);
        orderButton.setText("REKLAMLAR");
        orderButton.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) { 
                Log.i("ACTION","AD BUTTON CLICKED");
                try
                {
                    Intent i = new Intent(RestListActivity.this,AVMSystemActivity.class);   
                    startActivity(i);
                }
                catch(Exception e)
                {
                    Log.i("cort",e.toString());
                }
            } 
        });
        if(rl==null)
            Log.i("------------","RLOUT NULL AGA");
        else
            Log.i("------------","RLOUT VAR AGA");
        //************************************************************************************

//************************************************************************************
        for(int j=resIndex,f=0;j<resIndex+resCount;j++,f++)
        {
            try
            {
                Log.i("rest buttons","XXXXX");
                String name=a.getResourceName(j);
                name=name.substring(name.indexOf(':')+1);
                name=name.substring(9);
                if(name.startsWith("rest"))
                {
                    Log.i("BUTTON NAME",name);
                    buttons[f]=new ImageButton(this);
                    buttons[f].setImageDrawable(a.getDrawable(j));
                    buttons[f].setOnClickListener(new View.OnClickListener() { 
                        public void onClick(View view) { 
                            Log.i("ACTION","BUTTON CLICKED");
                        } 
                    });
                    //imageView.setImageDrawable(a.getDrawable(i));//------
                    //imageView.setBackgroundDrawable(a.getDrawable(i));
                    gv.addView(buttons[f], f);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
                break;
            }
        }
        if(rl==null)
            Log.i("RLRLRL","NULL");
        if(gv==null)
            Log.i("GVGVGV   ","NULL");
        rl.addView(gv);

    }


}

这是 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayoutRest"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <Button android:layout_alignParentLeft="true"
        android:layout_width="50px" android:id="@+id/adbutton"
        android:layout_height="fill_parent" android:text="R\nE\nK\nL\nA\nM\nL\nA\nR"
        android:textStyle="bold"></Button>
    <ScrollView android:id="@+id/scrollViewRest" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <LinearLayout android:id="@+id/linearLayoutRest" android:paddingLeft="50px" android:layout_height="fill_parent" android:layout_width="fill_parent"></LinearLayout>
    </ScrollView>
</RelativeLayout>

I implemented a system to have imagebuttons equal to the number of drawables (restaurant logos) so I filter the images starting with prefix "rest". Its ok up to here. Then I create a GridWiev and put the buttons to it and the root layout is a RelativeLayout. However it does not show any imagebuttons after compilation. There is my code and xml.
Thanks for any help.

public class RestListActivity extends Activity{
    ImageButton [] buttons;
    RelativeLayout rl;
    int restCount=0;
    Resources a;
    int resIndex=0;
    boolean resFound=false;
    int last;
    int resCount=0;
    GridView gv;
    // TODO Auto-generated constructor stub
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rl = (RelativeLayout) findViewById(R.id.relativeLayoutRest);
        if(rl==null)
            Log.i("***********","RLOUT NULL AGA");
        else
            Log.i("***********","RLOUT VAR AGA");
        setContentView(R.layout.restlist);
        Log.i("asd","asd");

        gv=new GridView(this);
        a=this.getResources();
        int i=0x7f020000;
        boolean done=true;
        if(rl==null)
            Log.i("***********","RLOUT NULL AGA");
        else
            Log.i("***********","RLOUT VAR AGA");
        Log.i("asd","asd");
        try
        {
            Log.i("rest buttons","______");
            for (i=0x7f020000 ;done&&i<0x7f020040;i++)
            {
                Log.i("rest buttons","----");
                String name=a.getResourceName(i);
                name=name.substring(name.indexOf(':')+1);
                name=name.substring(9);
                if(name.startsWith("rest"))
                {
                    if(!resFound)
                    {
                        resIndex=i;
                        resFound=true;
                    }
                    Log.i("rest buttons",name);

                    resCount++;
                }
            }
        }
        catch(Exception e)
        {
            last=i;
            Log.i("LAST",i+"");
            done=false;
            e.printStackTrace();
        }
        buttons=new ImageButton[resCount];
        Button orderButton = (Button)findViewById(R.id.adbutton);
        orderButton.setText("REKLAMLAR");
        orderButton.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) { 
                Log.i("ACTION","AD BUTTON CLICKED");
                try
                {
                    Intent i = new Intent(RestListActivity.this,AVMSystemActivity.class);   
                    startActivity(i);
                }
                catch(Exception e)
                {
                    Log.i("cort",e.toString());
                }
            } 
        });
        if(rl==null)
            Log.i("------------","RLOUT NULL AGA");
        else
            Log.i("------------","RLOUT VAR AGA");
        //************************************************************************************

//************************************************************************************
        for(int j=resIndex,f=0;j<resIndex+resCount;j++,f++)
        {
            try
            {
                Log.i("rest buttons","XXXXX");
                String name=a.getResourceName(j);
                name=name.substring(name.indexOf(':')+1);
                name=name.substring(9);
                if(name.startsWith("rest"))
                {
                    Log.i("BUTTON NAME",name);
                    buttons[f]=new ImageButton(this);
                    buttons[f].setImageDrawable(a.getDrawable(j));
                    buttons[f].setOnClickListener(new View.OnClickListener() { 
                        public void onClick(View view) { 
                            Log.i("ACTION","BUTTON CLICKED");
                        } 
                    });
                    //imageView.setImageDrawable(a.getDrawable(i));//------
                    //imageView.setBackgroundDrawable(a.getDrawable(i));
                    gv.addView(buttons[f], f);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
                break;
            }
        }
        if(rl==null)
            Log.i("RLRLRL","NULL");
        if(gv==null)
            Log.i("GVGVGV   ","NULL");
        rl.addView(gv);

    }


}

Here is the XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayoutRest"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <Button android:layout_alignParentLeft="true"
        android:layout_width="50px" android:id="@+id/adbutton"
        android:layout_height="fill_parent" android:text="R\nE\nK\nL\nA\nM\nL\nA\nR"
        android:textStyle="bold"></Button>
    <ScrollView android:id="@+id/scrollViewRest" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <LinearLayout android:id="@+id/linearLayoutRest" android:paddingLeft="50px" android:layout_height="fill_parent" android:layout_width="fill_parent"></LinearLayout>
    </ScrollView>
</RelativeLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心意如水 2024-12-04 01:48:25

写入这行

rl = (RelativeLayout) findViewById(R.id.relativeLayoutRest);

在这行 setContentView(R.layout.restlist); 后面

write this line

rl = (RelativeLayout) findViewById(R.id.relativeLayoutRest);

after this line setContentView(R.layout.restlist);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文