Android 布局问题的解决
如果我从代码创建布局,如何解决分辨率问题 我在我的手机上测试了它,它有320 x 480的分辨率,看起来还不错。但当我在我朋友的手机(Galaxy S)上测试时,它的布局似乎没有延伸 我之所以从代码中创建布局,是因为我不知道如何在特定位置放置按钮Android 布局问题的解决,android,layout,resolution,screen-resolution,Android,Layout,Resolution,Screen Resolution,如果我从代码创建布局,如何解决分辨率问题 我在我的手机上测试了它,它有320 x 480的分辨率,看起来还不错。但当我在我朋友的手机(Galaxy S)上测试时,它的布局似乎没有延伸 我之所以从代码中创建布局,是因为我不知道如何在特定位置放置按钮 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); rLayout = new RelativeLay
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。
是否取决于像素???您试图“绝对”地放置UI组件。货币贬值了,而你的问题正是原因所在。您可以为您的开发设备实现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"/>
我真的不明白。。这是否意味着仅仅将按钮放在我想要的位置将需要很多努力?这意味着,虽然自定义视图是可能的,甚至在某些情况下是可取的。。。如果你不知道自己在做什么,就应该避免使用它们。你需要努力去理解你想做的事情。