解决布局问题

发布于 2024-10-28 18:25:54 字数 2459 浏览 1 评论 0原文

如果我从代码创建布局,如何解决分辨率问题?

我在分辨率为 320 x 480 的手机上进行了测试,看起来不错。但是当我在我朋友的手机(Galaxy S)上测试它时,看起来布局没有伸展。

为什么我从代码创建布局是因为我不知道如何将按钮放置在特定位置。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    rLayout = new RelativeLayout(this);//(RelativeLayout)findViewById(R.id.relativeLayout1);
    rLayout.setBackgroundResource(R.drawable.menu_blankscreen);
    InitializeLayout();
    setContentView(rLayout);

}


private void InitializeLayout() {
    ImageButton newGame = new ImageButton(this);
    //newGame.setBackgroundResource(drawable.menu_button_new);
    newGame.setBackgroundResource(R.drawable.menu_button_new_xml);
    newGame.setOnClickListener(newGameClick);
    addView(newGame, 5, 174, 149, 41);

    ImageView continueGame = new ImageView(this);
    continueGame.setBackgroundResource(R.drawable.menu_button_continue_xml);
    continueGame.setOnClickListener(continueClick);
    addView(continueGame, 5, 218, 149, 41);

    ImageView highscore = new ImageView(this);
    highscore.setBackgroundResource(R.drawable.menu_button_highscore_xml);
    highscore.setOnClickListener(highscoreClick);
    addView(highscore, 5, 262, 149, 41);

    ImageView achievement = new ImageView(this);
    achievement.setBackgroundResource(R.drawable.menu_button_achievement_xml);
    achievement.setOnClickListener(achievementClick);
    addView(achievement, 5, 306, 149, 41);

    ImageView option = new ImageView(this);
    option.setBackgroundResource(R.drawable.menu_button_option_xml);
    option.setOnClickListener(optionClick);
    addView(option, 5, 350, 149, 41);

    ImageView quit = new ImageView(this);
    quit.setBackgroundResource(R.drawable.menu_button_quit_xml);
    quit.setOnClickListener(quitClick);
    addView(quit, 5, 395, 149, 41);

    Button btnTest = new Button(this);
    btnTest.setText("Test");
    btnTest.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(StartMenuActivity.this, TestActivity.class);
            startActivity(intent);
        }
    });
    addView(btnTest, 270, 430,50, 50);
}

public void addView(View view, int x, int y, int width, int height) {
    rParam = new RelativeLayout.LayoutParams(width, height);
    rParam.leftMargin = x;
    rParam.topMargin = y;
    rLayout.addView(view, rParam);
}

我假设 leftMargin 为 x,topMargin 为 y。 它与像素相关吗?

how's to solve the problem with resolution if I create layout from code?

I tested it on my phone which has 320 x 480 resolution and it's looks ok. but when I test it in my friends phone (Galaxy S) looks like the layout didn't stretch out.

Why I create the layout from code is because I don't know how to place buttons in specific location.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    rLayout = new RelativeLayout(this);//(RelativeLayout)findViewById(R.id.relativeLayout1);
    rLayout.setBackgroundResource(R.drawable.menu_blankscreen);
    InitializeLayout();
    setContentView(rLayout);

}


private void InitializeLayout() {
    ImageButton newGame = new ImageButton(this);
    //newGame.setBackgroundResource(drawable.menu_button_new);
    newGame.setBackgroundResource(R.drawable.menu_button_new_xml);
    newGame.setOnClickListener(newGameClick);
    addView(newGame, 5, 174, 149, 41);

    ImageView continueGame = new ImageView(this);
    continueGame.setBackgroundResource(R.drawable.menu_button_continue_xml);
    continueGame.setOnClickListener(continueClick);
    addView(continueGame, 5, 218, 149, 41);

    ImageView highscore = new ImageView(this);
    highscore.setBackgroundResource(R.drawable.menu_button_highscore_xml);
    highscore.setOnClickListener(highscoreClick);
    addView(highscore, 5, 262, 149, 41);

    ImageView achievement = new ImageView(this);
    achievement.setBackgroundResource(R.drawable.menu_button_achievement_xml);
    achievement.setOnClickListener(achievementClick);
    addView(achievement, 5, 306, 149, 41);

    ImageView option = new ImageView(this);
    option.setBackgroundResource(R.drawable.menu_button_option_xml);
    option.setOnClickListener(optionClick);
    addView(option, 5, 350, 149, 41);

    ImageView quit = new ImageView(this);
    quit.setBackgroundResource(R.drawable.menu_button_quit_xml);
    quit.setOnClickListener(quitClick);
    addView(quit, 5, 395, 149, 41);

    Button btnTest = new Button(this);
    btnTest.setText("Test");
    btnTest.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(StartMenuActivity.this, TestActivity.class);
            startActivity(intent);
        }
    });
    addView(btnTest, 270, 430,50, 50);
}

public void addView(View view, int x, int y, int width, int height) {
    rParam = new RelativeLayout.LayoutParams(width, height);
    rParam.leftMargin = x;
    rParam.topMargin = y;
    rLayout.addView(view, rParam);
}

I assume that leftMargin as x, and topMargin as y.
is it pixel dependant??

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

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

发布评论

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

评论(1

是伱的 2024-11-04 18:25:54

您正在尝试“绝对”放置 UI 组件。 AbsoluteLayout 已被贬值,您的问题正是原因。您可以为您的开发设备实现 UI 组件的“完美”放置。当它在具有不同像素密度或分辨率的另一台设备上运行时,或者......这样的例子不胜枚举,您将会遇到问题。

要在每个设备上实现完美的“绝对”放置既不实际也不可能。您可以使用各种布局并迭代属性中的不同值,让您的组件在大多数设备上真正非常接近像素完美。请阅读此处了解更多信息。

理解这个范式需要花费一些努力,但与 swing 相比,它实际上非常容易,至少对我来说是这样。

这就是我对纵向视图的看法:我们需要一个 LL 从上到下流动,放置在其中的每个组件都将位于下一个组件的上方或下方。然后我们需要另一个从左到右流动的 LL(嵌套)。每个组件都会被放置在每个组件的旁边。然后,根据您希望组件相对于像素屏幕密度和分辨率大小放置的位置,指定重力、边距、填充和权重。这是一个小例子,向您展示它的样子。希望有帮助!

<LinearLayout
      android:layout_width="match_parent"
      android:id="@+id/linearLayout1"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:gravity="center">
      <LinearLayout
          android:layout_width="match_parent"
          android:id="@+id/linearLayout2"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:gravity="center">
              <Button android:id="@+id/myButton"
                  style="@style/actionButton"
                  android:onClick="onClickFeature"
                  android:text="@string/title_button"
                  android:drawableTop="@drawable/pic_btn"
                  android:layout_marginBottom="5dip"
                  android:layout_marginTop="5dip"/>

You are trying to place UI components "absolutely". The AbsoluteLayout has been depreciated and your problem is precisely the reason. You can achieve "perfect" placement of UI components for your dev device. When its run on another device with a different pixel density or resolution or... the list goes on, you will have problems.

To achieve perfect, "absolute" placement on every device is neither practical nor possible. You can get your components really, really close to pixel perfect on most devices using the various layouts and iterating different values in the properties. Read here for more information.

It takes some effort to grok the paradigm, but it's actually really easy compared to swing, at least for me it is.

This is how I think for a portrait view: We need a LL to flow from top to bottom, every component placed inside will either be above or below the next. Then we need another LL (nested) that flows left to right. Every component will be placed to the side of each component. You then specify gravity, margin, padding and weight, based on where you want the component to be placed "relative" to pixel screen density and resolution size. Here is a little example to show you what it looks like. Hope it helps!

<LinearLayout
      android:layout_width="match_parent"
      android:id="@+id/linearLayout1"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:gravity="center">
      <LinearLayout
          android:layout_width="match_parent"
          android:id="@+id/linearLayout2"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:gravity="center">
              <Button android:id="@+id/myButton"
                  style="@style/actionButton"
                  android:onClick="onClickFeature"
                  android:text="@string/title_button"
                  android:drawableTop="@drawable/pic_btn"
                  android:layout_marginBottom="5dip"
                  android:layout_marginTop="5dip"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文