以编程方式使用多个按钮的Android设置按钮边距不起作用

时间:2013-10-29 15:57:25

标签: android button margin

我目前正在尝试实施一个Android应用,该应用有一个活动的6个按钮。当我尝试以编程方式设置按钮边距时,当只有一个按钮时,它没有任何问题。但是,当我尝试使用多个按钮时,所有这些按钮都设置在应用程序的左上角。我不知道该怎么办,所以任何帮助都会被证实。 这是布局和活动代码:

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>

1 个答案:

答案 0 :(得分:0)

您缺少的关键信息是因为您使用的是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具有各种功能。有关详细信息,请参阅此页面: http://developer.android.com/guide/topics/ui/layout/relative.html

有可能,您可能更喜欢不同的布局,例如垂直模式下的LinearLayout。假设您正在使用Eclipse,请使用UI,您将找到将RelativeLayout(默认值)转换为LinearLayout的方法。