在垂直方向的 LinearLayout 中垂直对齐 Button 和水平方向的 LinearLayout 时出现问题

发布于 2024-09-09 04:21:15 字数 5878 浏览 2 评论 0原文

我是通过手机发帖的,所以请原谅愚蠢的拼写错误和格式问题。

我有一个活动列出了玩家可以加载的已保存游戏。

我创建了一个简单的布局 xml 文件,它定义了 ScrollView。加载时,我抓取所有已保存的游戏,并以编程方式将每个已保存游戏的视图添加到 ScrollView 的垂直方向 LinearLayout 子项中。

每个游戏的视图由一个水平方向的 LinearLayout 组成,该布局又包含一个 Button 和一个垂直方向的 LinearLayout。该 LinearLayout 又包含一些 TextView 和 ImageView(以及另一个 LinearLayout,为了清楚起见,我在这里省略了它)。

层次结构看起来像这样(省略了一些细节)。

 ScrollView
      LinearLayout - vertical 
           Each saved game:
           LinearLayout - horizontal 
                Button - load game
                LinearLayout - vertical 
                     TextView - game name
                     TextView - date string

我的问题:

我希望按钮的顶部和“游戏名称”texview 垂直对齐,但 TextView (或者可能是 LinearLayout 父级)顶部有一些我不能的流氓填充摆脱。详细信息请参见屏幕截图。

alt

LoadSaved 类: 注意:mScrollView 的命名很糟糕。它指的是ScrollView的子LinearLayout。

   public class LoadSaved extends Activity {
     public LinearLayout mScrollView;
     private MinerDb mDb;

      public void onCreate(Bundle b) { 
             super.onCreate(b);
             setContentView(R.layout.loadsaved);
             mDb = new MinerDb(this);

            mScrollView = (LinearLayout) findViewById(R.id.load_scroll_view);
            Bundle[] savedGames = mDb.getSavedGames();

            for (int i = 0; i < savedGames.length; i++) {
                 Bundle game = savedGames[i];

                 final int gameId = game.getInt("gameId");
                 String name = game.getString("name");
                 String date = game.getString("date");

                 Bundle player = game.getBundle("player");
                 int playerMoney = player.getInt("money");
                 int playerHealth = player.getInt("health");

                 LinearLayout gameContainer = new LinearLayout(getApplicationContext());
                 gameContainer.setPadding(5, 5, 5, 5);
                 gameContainer.setGravity(Gravity.TOP);
                 gameContainer.setOrientation(LinearLayout.HORIZONTAL);
                 gameContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                 Button loadButton = new Button(getApplicationContext());
                 loadButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                 loadButton.setText("Load");


                 LinearLayout gameInfo = new LinearLayout(getApplicationContext());
                 gameInfo.setOrientation(LinearLayout.VERTICAL);
                 gameInfo.setPadding(10,0,10,10);
                 gameInfo.setGravity(Gravity.TOP);
                 gameInfo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                 TextView nameView = new TextView(getApplicationContext());
                 nameView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                 nameView.setGravity(Gravity.TOP);
                 nameView.setText(name);

                 TextView dateView = new TextView(getApplicationContext());
                 dateView.setPadding(5,0,0,0);
                 dateView.setGravity(Gravity.TOP);
                 dateView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                 dateView.setText(date);

                 LinearLayout playerView = new LinearLayout(getApplicationContext());
                 playerView.setOrientation(LinearLayout.HORIZONTAL);
                 playerView.setPadding(5,0,0,0);
                 playerView.setGravity(Gravity.TOP);
                 playerView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                 TextView playerMoneyView = new TextView(getApplicationContext());
                 playerMoneyView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                 playerMoneyView.setPadding(0,0,10,0);
                 playerMoneyView.setTextColor(Color.GREEN);
                 playerMoneyView.setText("$" + playerMoney);

                TextView playerHealthView = new TextView(getApplicationContext());
                playerHealthView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                playerHealthView.setPadding(0,0,10,0);
                playerHealthView.setTextColor(Color.RED);
                playerHealthView.setText(playerHealth + "%");

                playerView.addView(playerMoneyView);
                playerView.addView(playerHealthView);

                gameInfo.addView(nameView);
                gameInfo.addView(dateView);
                gameInfo.addView(playerView);

                gameContainer.addView(loadButton);
                gameContainer.addView(gameInfo);

                mScrollView.addView(gameContainer);

                loadButton.setOnClickListener(new OnClickListener() { 
                     public void onClick(View v) {
                           Log.e("LoadSaved", "LoadSaved::onCreate: Clicking: " + gameId);
                           Intent loadGameIntent = new Intent(LoadSaved.this, Miner.class);
                           loadGameIntent.putExtra("load_game", gameId);
                           startActivity(loadGameIntent);
                           finish();
                      }
                 });
            }
       }
  }

加载保存的.xml

           <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/load_scroll_view" />

       </ScrollView>
  </LinearLayout>

Im posting from my phone so please excuse stupid typos and formatting issues.

I have an activity which lists saved games that the player can load.

I created a simple layout xml file which defines a ScrollView. On load, I grab all the saved games and programatically add a view for each saved game to a vertically oriented LinearLayout child of the ScrollView.

The view for each game consists of a Horizontally oriented LinearLayout which in turn contains a Button and a vertically oriented LinearLayout. That LinearLayout in turn contains some TextViews and ImageViews (and one more LinearLayout which I'm ommitting here for the sake of clarity).

The hierarchy looks something like this (some details omitted).

 ScrollView
      LinearLayout - vertical 
           Each saved game:
           LinearLayout - horizontal 
                Button - load game
                LinearLayout - vertical 
                     TextView - game name
                     TextView - date string

My problem:

I would like the top of the button and the "game name" texview to be vertically aligned but the TextView (or maybe it's LinearLayout parent) has some rogue padding on top that I can't get rid of. See screenshot for details.

alt

LoadSaved class:
Note: mScrollView is badly named. It refers to the ScrollView's child LinearLayout.

   public class LoadSaved extends Activity {
     public LinearLayout mScrollView;
     private MinerDb mDb;

      public void onCreate(Bundle b) { 
             super.onCreate(b);
             setContentView(R.layout.loadsaved);
             mDb = new MinerDb(this);

            mScrollView = (LinearLayout) findViewById(R.id.load_scroll_view);
            Bundle[] savedGames = mDb.getSavedGames();

            for (int i = 0; i < savedGames.length; i++) {
                 Bundle game = savedGames[i];

                 final int gameId = game.getInt("gameId");
                 String name = game.getString("name");
                 String date = game.getString("date");

                 Bundle player = game.getBundle("player");
                 int playerMoney = player.getInt("money");
                 int playerHealth = player.getInt("health");

                 LinearLayout gameContainer = new LinearLayout(getApplicationContext());
                 gameContainer.setPadding(5, 5, 5, 5);
                 gameContainer.setGravity(Gravity.TOP);
                 gameContainer.setOrientation(LinearLayout.HORIZONTAL);
                 gameContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                 Button loadButton = new Button(getApplicationContext());
                 loadButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                 loadButton.setText("Load");


                 LinearLayout gameInfo = new LinearLayout(getApplicationContext());
                 gameInfo.setOrientation(LinearLayout.VERTICAL);
                 gameInfo.setPadding(10,0,10,10);
                 gameInfo.setGravity(Gravity.TOP);
                 gameInfo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

                 TextView nameView = new TextView(getApplicationContext());
                 nameView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                 nameView.setGravity(Gravity.TOP);
                 nameView.setText(name);

                 TextView dateView = new TextView(getApplicationContext());
                 dateView.setPadding(5,0,0,0);
                 dateView.setGravity(Gravity.TOP);
                 dateView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                 dateView.setText(date);

                 LinearLayout playerView = new LinearLayout(getApplicationContext());
                 playerView.setOrientation(LinearLayout.HORIZONTAL);
                 playerView.setPadding(5,0,0,0);
                 playerView.setGravity(Gravity.TOP);
                 playerView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

                 TextView playerMoneyView = new TextView(getApplicationContext());
                 playerMoneyView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                 playerMoneyView.setPadding(0,0,10,0);
                 playerMoneyView.setTextColor(Color.GREEN);
                 playerMoneyView.setText("$" + playerMoney);

                TextView playerHealthView = new TextView(getApplicationContext());
                playerHealthView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                playerHealthView.setPadding(0,0,10,0);
                playerHealthView.setTextColor(Color.RED);
                playerHealthView.setText(playerHealth + "%");

                playerView.addView(playerMoneyView);
                playerView.addView(playerHealthView);

                gameInfo.addView(nameView);
                gameInfo.addView(dateView);
                gameInfo.addView(playerView);

                gameContainer.addView(loadButton);
                gameContainer.addView(gameInfo);

                mScrollView.addView(gameContainer);

                loadButton.setOnClickListener(new OnClickListener() { 
                     public void onClick(View v) {
                           Log.e("LoadSaved", "LoadSaved::onCreate: Clicking: " + gameId);
                           Intent loadGameIntent = new Intent(LoadSaved.this, Miner.class);
                           loadGameIntent.putExtra("load_game", gameId);
                           startActivity(loadGameIntent);
                           finish();
                      }
                 });
            }
       }
  }

loadsaved.xml

           <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/load_scroll_view" />

       </ScrollView>
  </LinearLayout>

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

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

发布评论

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

评论(3

格子衫的從容 2024-09-16 04:21:15

如果你想要任何类型的对齐,为什么不使用RelativeLayout呢?这基本上是为了将一个视图与另一个视图对齐而设计的。 android:layout_alignTop 听起来像你想要的东西。

(当然,还要验证所有控件中的填充值是否相同,但我确信您已经这样做了。)

If you want any kind of alignment, why don't you use a RelativeLayout? That's basically designed to align one view with another. android:layout_alignTop sounds like something you want.

(And, of course, verify that the padding values are the same in all controls, but I'm sure you did that.)

怀中猫帐中妖 2024-09-16 04:21:15

为什么不尝试使用 ListView 来实现这种 gui。

您仍然需要定义行 xml。

Why don't you try using a ListView for that kind of gui.

You will still need to define a row xml.

凉城凉梦凉人心 2024-09-16 04:21:15

+1建议ListView和RelativeLayout的答案。对于这种类型的情况,您可能需要一个带有使用RelativeLayout 的项目布局的ListView。 (如果有很多项目,ListView 的缩放效果会更好,如果这是用于已保存游戏的列表,则似乎可能会增长很多。)对于这种类型的 UI,建议让整个行/列表项可单击,而不是而不是使用一个小的“加载”按钮,但这是一个设计问题,最终取决于您。

不要使用 getApplicationContext 创建视图。 Activity 是一个上下文,只需在您的情况下传递 this 即可。

默认情况下,LinearLayouts 尝试通过文本基线(如果存在)对齐子视图。请注意,按钮的“加载”文本底部与屏幕截图中的“CURRENT_GAME”文本完美对齐。尝试gameContainer.setBaselineAligned(false)

通常,如果您设置了baselineAlignedChildIndex,您的gameInfo布局只会报告其子级之一的基线,但在以编程方式创建LinearLayouts时,cupcake和eclair之间的这种行为似乎发生了变化。 (链接到在 AOSP 中更改它的提交 这里。)

+1 to the answers suggesting ListView and RelativeLayout. For this type of situation you probably want a ListView with an item layout using RelativeLayout. (ListView will scale much better if there are many items, and if this is for a list of saved games it seems like this could grow quite a bit.) For this type of UI it's recommended to have the whole row/list item clickable rather than use a small Load button, but that's a design issue and ultimately up to you.

Don't use getApplicationContext for creating your views. Activity is a Context, just pass this in your case.

By default LinearLayouts try to align child views by their text baseline if present. Note that the bottom of your button's Load text aligns perfectly with the CURRENT_GAME text in your screenshot. Try gameContainer.setBaselineAligned(false).

Normally your gameInfo layout would only report the baseline of one of its children if you set a baselineAlignedChildIndex, but it looks like this behavior changed between cupcake and eclair when creating LinearLayouts programmatically. (Link to the commit that changed it in AOSP here.)

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