【问题标题】:Android setting button margins programmatically with multiple buttons does not workAndroid 使用多个按钮以编程方式设置按钮边距不起作用
【发布时间】:2013-11-08 21:46:22
【问题描述】:

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

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>

【问题讨论】:

    标签: android button margin


    【解决方案1】:

    您缺少的关键信息是,由于您使用的是 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 的方法。

    【讨论】:

    • 感谢回复,我一到家就看。实际上,我的主要问题是将按钮设置为背景上的特定想法,(它们也必须针对不同的屏幕尺寸保留)这就是我尝试以编程方式设置按钮的原因。
    • 恐怕没有用,史蒂文。整个按钮也消失了,因为你写了 android:layout_below="@id/main_bg"
    • 您的 ImageView 可能会占用屏幕。将宽度和高度更改为固定值。
    猜你喜欢
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    相关资源
    最近更新 更多