【问题标题】:How to add a button programmatically?如何以编程方式添加按钮?
【发布时间】:2016-05-25 14:03:20
【问题描述】:

我尝试向我的 RelativLayout 添加一个按钮。 它有效,但按钮相互重叠。 我想我是在覆盖 LayoutParams,或者我使用了错误的方法,但我不知道。

这只是一个测试。在最终版本中,我想将未定义数量的 Button 添加到带有 relativLayout 的滚动视图中。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    Button myButton = new Button(MainActivity.this);
                    myButton.setText("Push Me");

                    RelativeLayout ll = (RelativeLayout)findViewById(R.id.relative_main);
                    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                    lp.topMargin = R.id.button;
                    myButton.setLayoutParams(lp);
                    ll.addView(myButton);

                }
            }
    );
}
}

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.freddy.test.MainActivity"
android:id="@+id/relative_main">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add new Button"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />
</RelativeLayout>

【问题讨论】:

    标签: android button android-relativelayout


    【解决方案1】:

    这些按钮相互重叠,因为您使用的是 RelativeLayout 而没有告诉按钮应该相对于布局的哪个位置。 可能对您有所帮助的方法是使用 LinearLayout 而不是 RelativeLayout 并向其中添加按钮。

    【讨论】:

      【解决方案2】:

      尝试使用它而不是 FrameLayout.LayoutParams 这使您可以更好地控制相对布局子级(因为您的父布局是 RelativeLayout)

      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      

      您可以通过以下方式与另一个视图对齐: params.addRule(RelativeLayout.BELOW, R.id.putyourviewhere);

      如果您坚持使用 RelativeLayout。但是 LinearLayout 的解决方案也可以。但是RelativeLayout有更多的控制权

      【讨论】:

        【解决方案3】:

        当您使用RelativeLayout 时,如果您希望按钮水平排列,则必须使用toRightOF,如果您希望按钮垂直排列,则必须使用below ,因此要以编程方式添加其中任何一个,只需将其作为规则添加到您的layoutParams:

        lp.addRule(RelativeLayout.RIGHT_OF, R.id.button);
        

        或

        lp.addRule(RelativeLayout.BELOW, R.id.button);
        

        【讨论】:

        • 太棒了 :) 所以请接受我的回答,因为它解决了您的问题
        【解决方案4】:

        但这只是一个测试,在最终版本中我想添加一个 未定义数量的按钮到带有相对布局的滚动视图 它。

        如果你的问题是这样的

        按钮相互重叠

        解决起来比较简单。请改用&lt;LinearLayout&gt; 在您的根标签中添加一个名为android:orientation="vertical" 或android:orientation="horizontal" 的属性。

        RelativeLayout 也有一些属性可以解决这个问题,但它有点复杂。 如果这些是你经常遇到的问题,我建议你参加this课程。

        示例:

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
        
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">
        
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
        
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
        
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
        
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
        
                </LinearLayout>
        
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal">
        
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
        
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
        
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
        
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
        
                </LinearLayout>
        
            </LinearLayout>
        
        </ScrollView>
        

        【讨论】:

        • 在最终版本中,一行中有 4 个按钮,仅适用于 RelativLayout,我想添加未定义数量的这些行
        • 我添加了一个示例,它应该也适用于 LinearLayout,但我不确定这是否是您的意思。
        • 好的,谢谢伙计,但我已经找到了解决方案,但做得很好!
        【解决方案5】:

        好的,现在尝试添加其他参数来控制布局,例如居中 CENTER_HORIZONTAL

        params.addRule(RelativeLayout.CENTER_HORIZONTAL, -1); // -1 means that CENTER_HORIZONTAL doesn't take a view as a parameter
        

        【讨论】:

          【解决方案6】:
          FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
          
          
          lp.topMargin = R.id.button;
          // use some meaningful margin, R.id.button can return any integer value. 
          
          
          // you have issue with alignment of button
          
          ll.addView(myButton);
          // with this call your button should be in view hierarchy now you have to align your button. best is use RelativeLayout.LayoutParams and make your button relative(top/bottom/left/right) to some other component in viewgroup(RelativeLayout). 
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-06-30
            • 2012-06-20
            • 1970-01-01
            • 2020-09-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多