2016-05-25 15 views
0

私はRelativLayoutにButtonを追加しようとしています。 これは機能しますが、ボタンは互いに重なっています。 LayoutParamsをオーバーライドしていると思います。間違ったメソッドを使用していますが、わかりません。プログラムでボタンを追加するにはどうすればよいですか?

これは単なるテストに過ぎません。最後のバージョンでは、scrollViewに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> 

答えて

0

あなたはどの場所で告げずにRelativeLayoutを使用しているため、ボタンが重なっているが、レイアウトに比べて行くことになっボタンです。 RelativeLayoutの代わりにLinearLayoutを使用してボタンを追加するのに役立つものがあります。

0

しかし、これは私が にrelativLayoutとscrollviewにそれをボタンの 未定義の数を追加したい最終バージョンでのみテスト、です。

あなたの問題は

ボタンが

重なっていることを解決するために比較的簡単であるということであるならば。代わりに <LinearLayout>を使用してください ルートタグには、 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> 
+0

を中心に同じようなレイアウトを制御するために、別のパラメータを追加しようと、それは唯一のRelativLayoutで動作し、私は追加したいですこれらの行の定義されていない数 – Locki

+0

例を追加しましたが、LinearLayoutでも動作するはずですが、それが意味するものであればわかりません。 –

+0

ありがとう、しかし、私はすでに解決策を見つけましたが、素晴らしい仕事! – Locki

0

することができます

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

(自分の親のレイアウトはRelativeLayoutあるので)これはあなたの相対的なレイアウトの子供たちをより詳細に制御できます代わりにFrameLayout.LayoutParams のこれを使用してみてください次のようにして別のビューに揃えます。 params.addRule(RelativeLayout.BELOW、R.id.putyourviewhere);

これはRelativeLayoutに固執している場合です。しかし、LinearLayoutの解決法もOKです。あなたはボタンが並ぶことにしたい場合、あなたはボタンが水平またはbelowを並ぶことにしたい場合はtoRightOFを使用する必要が垂直ので、のいずれかを追加するRelativeLayoutを使用しているとしてではなく、RelativeLayoutは、より多くの制御

+0

ありがとうございました! – Locki

0

を持っていますそれらは、プログラム、ちょうどあなたのlayoutParamsにルールとして追加:

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

OR

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

ありがとうございました! – Locki

+0

素晴らしい:)それはあなたの問題を解決したように親切に私の答えを受け入れる –

0
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). 
0

OKは、今の行の4つのボタンがあり、最終版ではCENTER_HORIZONTAL

params.addRule(RelativeLayout.CENTER_HORIZONTAL, -1); // -1 means that CENTER_HORIZONTAL doesn't take a view as a parameter 
関連する問題