2012-04-11 2 views
1

Моего layout.xml файл выглядит следующим образом:Android динамического RelativeLayout

<ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/scrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="270dp" 
     android:layout_weight="0.47" > 

     <LinearLayout 
      android:id="@+id/linearLayout2" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 
     </LinearLayout> 

    </ScrollView> 

Я использую следующий код для создания TextViewButton с и S внутри for цикла:

View linearLayout = findViewById(R.id.linearLayout2); 


for (int i=0 ; i<10; i++){ 
    TextView textview = new TextView(this); 
    textview.setText("Text view" + i); 
    textview.setId(i); 

    textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
    ((LinearLayout) linearLayout).addView(textview); 


    Button button = new Button(this); 
    button.setText("View"); 
    button.setId(i); 
    int width=90; 
    int height=60; 
    button.setLayoutParams(new LayoutParams(width, height)); 
    ((LinearLayout) linearLayout).addView(button); 

} 

Я получаю Button ниже TextView. Может ли кто-нибудь помочь мне с заменой этого LinearLayout на RelativeLayout, так что я получаю TextView и Button бок о бок?

ответ

5

Изменить макет таким образом у вас есть RelativeLayout вместо LinearLayout:

<ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/scrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="270dp" 
     android:layout_weight="0.47" > 

     <RelativeLayout 
      android:id="@+id/relativeLayout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      > 
     </RelativeLayout> 

</ScrollView> 

Затем попробуйте сделать это, чтобы сделать TextView s и Button сек сидят на ту же строку:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout); 
    for (int i = 0; i < 10; i++) { 
     Button button = new Button(this); 
     button.setText("View" + i); 
     button.setId(1000 + i); 
     int width = 90; 
     int height = 60; 

     TextView textview = new TextView(this); 
     textview.setText("Text view" + i); 
     textview.setId(2000 + i); 

     if (i == 0) { 
      RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.FILL_PARENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT); 
      rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
      rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
      textview.setLayoutParams(rlp2); 
      rl.addView(textview); 
      RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
        width, height); 
      rlp1.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
      rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
      button.setLayoutParams(rlp1); 
      rl.addView(button); 
     } else { 
      RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.FILL_PARENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT); 
      rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
      rlp2.addRule(RelativeLayout.BELOW, button.getId() - 1); 
      textview.setLayoutParams(rlp2); 
      rl.addView(textview); 
      RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
        width, height); 
      rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
      rlp1.addRule(RelativeLayout.BELOW, button.getId() - 1); 
      button.setLayoutParams(rlp1); 
      rl.addView(button); 
     } 
    } 
+0

работает отлично .. спасибо очень сэр .. –

+0

привет сэр .. теперь мне нужно добавить слушателя onclick для каждой кнопки на основе идентификатора i, полученного из цикла for ... вы можете дать мне предложение о том, как добиться этого PLS ... –

0
View linearLayout = findViewById(R.id.linearLayout2); 


for (int i=0 ; i<10; i++){ 
    TextView textview = new TextView(this); 
    textview.setText("Text view" + i); 
    textview.setId(i); 

    textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
    ((LinearLayout) linearLayout).addView(textview); 


    Button button = new Button(this); 
    button.setText("View"); 
    button.setId(i); 
    int width=90; 
    int height=60; 
    button.setLayoutParams(new LayoutParams(width, height)); 
    ((LinearLayout) linearLayout).addView(button); 

} 

заменить выше ниже код ::

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout2); 
for (int i=0 ; i<10; i++){ 
LinearLayout l = new LinearLayout(this); 
l.setOrientation(LinearLayout.HORIZONTAL); 

TextView textview = new TextView(this); 
textview.setText("Text view" + i); 
textview.setId(i); 

l.addView(textview); 
Button button = new Button(this); 
button.setText("View"); 
button.setId(i); 
button.setWidth(90); 
button.setHeight(60); 
l.addView(button); 
linearLayout.addView(l);//if you want you can layout params linearlayout 
} 
+0

не работает .. только отображается пустая страница .. –

+0

Попробовать изменился небольшое –

+0

извини дружище .. нет улучшения .. до сих пор он показывает пустую страницу. . –

0

Вы определяете LinearLayout с android:orientation="vertical", так что это будет, как правило, один ниже другого.

Добавьте текст и кнопку в отдельные линейные макеты с ориентацией по горизонтали и добавьте этот linearLayout в исходный вертикальный LinearLayout.

Edit: Попробуйте следующее:

View linearLayout = findViewById(R.id.linearLayout2); 

for (int i=0 ; i<10; i++){ 
View indiLyt = new View(this); 
indiLyt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
indiLyt.setOrientation(LinearLayout.HORIZONTAL); 

TextView textview = new TextView(this); 
textview.setText("Text view" + i); 
textview.setId(i); 

textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
((LinearLayout) indiLyt).addView(textview); 

Button button = new Button(this); 
button.setText("View"); 
button.setId(i); 
int width=90; 
int height=60; 
button.setLayoutParams(new LayoutParams(width, height)); 
((LinearLayout) indiLyt).addView(button); 
((LinearLayout) linearLayout).addView(indiLyt); 
} 
+0

кажется, что это не сработало для него –

0

попробовать это решение ::;

есть новый xml-файл, как показано ниже в res/layout вашего проекта.

Сохранить как linear.xml

< LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/linear" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/text" 
      android:layout_width=" wrap_content " 
      android:layout_height=" wrap_content " /> 
<Button   android:id="@+id/button" 
      android:layout_width="90dp" 
      android:layout_height="60dp" /> 
    </ LinearLayout > 

и код ниже :::

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout2); 
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService 
     (Context.LAYOUT_INFLATER_SERVICE); 

for (int i=0 ; i<10; i++){ 
View view = inflater.inflate(R.layout.linear,null); 
view.findViewById(R.id.text).setText("Text view" + i); 
view.findViewById(R.id.text).setTextColor(Color.BLUE); 
view.findViewById(R.id.button).setText(“View"); 
view.findViewById(R.id.button).setTextColor(Color.BLUE); 
linearLayout.addView(view); 
} 
+0

большое спасибо за вашу помощь .. получил решение от предложения Luksprog ... –

Смежные вопросы