将 xml 文件包装在relativelayout中 setContentView(R.layout.activity_main); -->新的相对布局(这个);

发布于 2025-01-19 14:08:31 字数 1821 浏览 1 评论 0原文

我想将activity_xml 包装在RelativeLayout 中,我尝试了几种方法,但没有起作用。是否可以? 我的叫做 gameButtons --> gameButtons.addView(#问题如何在此处添加activity_xml#);

public class Game extends AppCompatActivity {

    GamePanel gameView; // The class extending SurfaceView
    FrameLayout game; // Holder between SurfaceView and RelativeLayout
    RelativeLayout gameButtons; // Where I add the buttons
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Creating Layout Objects
        gameView = new GamePanel(this);
        game = new FrameLayout(this);
        gameButtons = new RelativeLayout(this);
        ContentView activityView = ContentView(R.layout.activity_main);

        RelativeLayout.LayoutParams activityMain = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        //Define the layout parameter for whatever we want to wrap both width and heigth
        RelativeLayout.LayoutParams parameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
        // Matching with the content we want to add

        gameButtons.setLayoutParams(parameters);
        gameButtons.addView(activityView); << ---- PROBLEM !!


        //setContentView(R.layout.activity_main);
        setContentView(new GamePanel(this));
    }
}

我希望能够通过相对布局显示来自 Activity_main 的按钮和其他内容。

我需要使用按钮的意图函数 = Activity_xml 中的按钮

我尝试了这些gameButtons.addView(activity_xml);

*gameButtons.addView(ContentView(R.layout.activity_main));

*gameButtons.addView(R.layout.activity_main);

*gameButtons.addView(activityView);

*// 我玩ContentView的地方activityView = ContentView(R.layout.activity_main);

I want to wrap activity_xml in a RelativeLayout, I tried several ways, bit non is working. Is it possible?
Mine is called gameButtons --> gameButtons.addView(#problem how to add activity_xml here#);

public class Game extends AppCompatActivity {

    GamePanel gameView; // The class extending SurfaceView
    FrameLayout game; // Holder between SurfaceView and RelativeLayout
    RelativeLayout gameButtons; // Where I add the buttons
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Creating Layout Objects
        gameView = new GamePanel(this);
        game = new FrameLayout(this);
        gameButtons = new RelativeLayout(this);
        ContentView activityView = ContentView(R.layout.activity_main);

        RelativeLayout.LayoutParams activityMain = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        //Define the layout parameter for whatever we want to wrap both width and heigth
        RelativeLayout.LayoutParams parameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
        // Matching with the content we want to add

        gameButtons.setLayoutParams(parameters);
        gameButtons.addView(activityView); << ---- PROBLEM !!


        //setContentView(R.layout.activity_main);
        setContentView(new GamePanel(this));
    }
}

I expected the posibility to both show buttons from activity_main and other content under via the RelativeLayout..

I need to use the intent functions for buttons = the buttons in activity_xml

I tried thesegameButtons.addView(activity_xml);

*gameButtons.addView(ContentView(R.layout.activity_main));

*gameButtons.addView(R.layout.activity_main);

*gameButtons.addView(activityView);

*// where I played with ContentView activityView = ContentView(R.layout.activity_main);

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

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

发布评论

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

评论(1

抚笙 2025-01-26 14:08:31

所以新手编码器找到了解决方案! = 那就是我!

LayoutInflater inflater = /*LayoutInflater.from(YourActivity.this); // or */(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View viewMyLayout = inflater.inflate(R.layout.activity_main, null);

就我而言,

LayoutInflater inflater = (LayoutInflater) 

getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View viewMyLayout = inflater.inflate(R.layout.activity_main, null);

我将其添加到

gameButtons.setLayoutParams(parameters);

gameButtons.addView(viewMyLayout); // <---- HERE!!
    
game.addView(gameView);
game.addView(gameButtons);
    
setContentView(game);

现在它可以工作了!

So the newbie coder found the solution!! = That is me!

LayoutInflater inflater = /*LayoutInflater.from(YourActivity.this); // or */(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View viewMyLayout = inflater.inflate(R.layout.activity_main, null);

in my case

LayoutInflater inflater = (LayoutInflater) 

getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View viewMyLayout = inflater.inflate(R.layout.activity_main, null);

I added that in

gameButtons.setLayoutParams(parameters);

gameButtons.addView(viewMyLayout); // <---- HERE!!
    
game.addView(gameView);
game.addView(gameButtons);
    
setContentView(game);

And now it works!

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