Android使用多个按钮以编程方式设置按钮边距不起作用
我目前正在尝试实现一个Android应用程序,一个活动有6个按钮。当我试图以编程方式设置按钮边距时,当只有一个buton时,它设置没有任何问题。但是,当我尝试使用多个按钮时,所有按钮都设置在应用程序的左上角。我不知道该怎么办,所以任何帮助都会得到报答。 以下是布局和活动代码:Android使用多个按钮以编程方式设置按钮边距不起作用,android,button,margin,Android,Button,Margin,我目前正在尝试实现一个Android应用程序,一个活动有6个按钮。当我试图以编程方式设置按钮边距时,当只有一个buton时,它设置没有任何问题。但是,当我尝试使用多个按钮时,所有按钮都设置在应用程序的左上角。我不知道该怎么办,所以任何帮助都会得到报答。 以下是布局和活动代码: private void initializeUIElements() { Display display = getWindowManager().getDefaultDisplay(); int
private void initializeUIElements()
{
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
playButton = (Button) (findViewById(R.id.play_button));
final ViewGroup.MarginLayoutParams lpt =(MarginLayoutParams)playButton.getLayoutParams();
lpt.setMargins((int)(width/2.4),(height/4),(int)(width/2.4),(height/4));
playButton.setLayoutParams(lpt);
facebookButton = (Button) (findViewById(R.id.facebook_button));
lpt.setMargins(100,400,380,400);
facebookButton.setLayoutParams(lpt);
t = (TextView) (findViewById(R.id.textView1));
t.setText(width + " " + height);
twitterButton = (Button) (findViewById(R.id.twitter_button));
topTenButton = (Button) (findViewById(R.id.top_ten_button));
yayinAkisiButton = (Button) (findViewById(R.id.yayin_akisi_button));
son5Button = (Button) (findViewById(R.id.son_5_button));
playButton.setOnClickListener(this);
facebookButton.setOnClickListener(this);
twitterButton.setOnClickListener(this);
topTenButton.setOnClickListener(this);
yayinAkisiButton.setOnClickListener(this);
son5Button.setOnClickListener(this);
}
以下是XML布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/main_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/bg" />
<Button
android:id="@+id/facebook_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Facebook"
android:onClick="true"/>
<Button
android:id="@+id/son_5_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Son 5" />
<Button
android:id="@+id/yayin_akisi_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yayın Akışı" />
<Button
android:id="@+id/top_ten_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TopTen" />
<Button
android:id="@+id/twitter_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Twitter" />
<Button
android:id="@+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="193dp"
android:text="TextView" />
</RelativeLayout>
您缺少的关键信息是,由于您使用的是RelativeLayout,因此必须指定每个按钮相对于其他按钮的显示规则 请按如下方式修改您的XML:
<Button
android:id="@+id/facebook_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Facebook"
android:onClick="true"
android:layout_below="@id/main_bg" (this is what you are missing)
/>
RelativeLayouts有各种各样的能力。有关更多信息,请参阅本页:
您可能更喜欢不同的布局,例如垂直模式下的线性布局。假设您使用的是Eclipse,使用UI,您将找到一种将RelativeLayout(默认设置)转换为LinearLayout的方法 谢谢你的回复,我一到家就看。事实上,我的主要问题是在我的背景中为特定的想法设置按钮(而且它们必须保持不同的屏幕大小),这就是为什么我尝试以编程方式设置按钮的原因。我担心这不起作用。整个按钮也消失了,因为你在下面写了android:layout_=“@id/main_bg”,你的ImageView可能占据了屏幕。将宽度和高度更改为固定值。