###[Android] 動的に生成したTextViewのmarginが機能しない
動的にTextViewを生成して、
drawble.xml の <shape> の装飾をかけたいと考えています。
なぜか margin が効いておらず、
上下の間隔を開けたいのですが、どうすればいいでしょうか?
###該当のソースコード
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout layout = (LinearLayout)findViewById(R.id.layout); List<String> list = new ArrayList<>(); list.add("Hello 1"); list.add("Hello 2"); list.add("Hello 3"); for(int i = 0; i < list.size(); i++){ TextView textView = new TextView(this); textView.setBackgroundResource(R.drawable.border); textView.setText(list.get(i)); layout.addView(textView); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.blue.samplemarginproject.MainActivity"> <LinearLayout android:id="@+id/layout" android:layout_width="wrap_content" android:layout_height="0dp" android:orientation="vertical" tools:layout_constraintTop_creator="1" tools:layout_constraintRight_creator="1" tools:layout_constraintBottom_creator="1" android:layout_marginStart="8dp" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginEnd="8dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="8dp" tools:layout_constraintLeft_creator="1" android:layout_marginBottom="8dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent"></LinearLayout> </android.support.constraint.ConstraintLayout>
res/drawable/border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <stroke android:width="3px" android:color="#CCCCCC" /> <size android:width="240dp" android:height="64dp" /> <padding android:left="20dp" android:top="20dp" android:right="20dp" android:bottom="20dp" /> <margin android:left="0dp" android:top="20dp" android:right="0dp" android:bottom="50dp"/> <corners android:radius="10px" /> </shape>
###補足情報(言語/FW/ツール等のバージョンなど)
Android Studio 2.3.2
API 21: Android 5.0 (Lollipop)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。