2011-12-30 2 views
0

Я создал динамический TableLayout, используя источник в классе java (DynamicTableLayout.java). Используя этот кодКак передать dymanic tablelayout в xml-форму в linearlayout?

public class DynamicTableLayout extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    TableLayout layout = new TableLayout (this); 
    layout.setLayoutParams(new TableLayout.LayoutParams(4,3)); 

    TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
      ViewGroup.LayoutParams.FILL_PARENT, 
      ViewGroup.LayoutParams.FILL_PARENT, 
      1.0f); 

    rowLp.setMargins(6, 0, 0,0); 

    TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
      ViewGroup.LayoutParams.FILL_PARENT, 
      ViewGroup.LayoutParams.FILL_PARENT, 
      1.0f); 

    cellLp.setMargins(6,0, 0, 0); 

    layout.setPadding(3,1,1,1); 

    for (int f=0; f<=6; f++) { 

     TableRow tr = new TableRow(this); 

     tr.setBackgroundColor(Color.BLACK); 
     tr.setPadding(1,1,1,1); 

     TableRow.LayoutParams llp = new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); 
     llp.setMargins(2, 0, 0, 0); 




     for (int c=0; c<=3; c++) { 

      LinearLayout cell = new LinearLayout(this); 
      cell.setBackgroundColor(Color.WHITE); 
      cell.setLayoutParams(llp);//2px border on the right for the cell 

      TextView b = new TextView (this); 
      b.setText("Sample"); 
      b.setTextSize(10.0f); 

      b.setPadding(0, 0, 0, 0); 

      cell.addView(b); 
      tr.addView(cell,cellLp); 

     } 
     layout.addView(tr,rowLp); 

    } 

    super.setContentView(layout); 
} } 

main.xml код

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
    android:id="@+id/linear1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/bottom_black_pane" 
    android:orientation="horizontal"> 

    <TextView 
    android:id="@+id/textview1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Color range" 
    android:gravity="center" 
    android:textSize="20dp" 
    android:textColor="#ffffff" 
    android:paddingTop="10dp"/> 
     </LinearLayout> 

    <LinearLayout 
android:id="@+id/linear2" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 

    <ImageView android:id="@+id/imageView1" 
    android:src="@drawable/palette" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    </ImageView> 
</LinearLayout> 

<LinearLayout 
android:id="@+id/linear2" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
</LinearLayout> 

<LinearLayout 
android:id="@+id/linear3" 
android:layout_height="wrap_content" 
android:background="@drawable/bottom_black_pane" 
android:layout_width="wrap_content"> 

<LinearLayout 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:weightSum="1" android:layout_width="355dp"> 

<Button 
android:id="@+id/Btn1" 
android:layout_width="30dp" 
android:text="All" 
android:paddingTop="10dp" 
android:layout_height="wrap_content" 
android:textColor="#ffffff" 
android:background="#000000" 
android:layout_weight="0.18"/> 

<Button android:layout_width="30dp" 
    android:layout_weight="0.18" 
    android:textColor="#ffffff" 
    android:background="#000000" 
    android:paddingTop="10dp" 
    android:id="@+id/Btn2" 
    android:layout_height="wrap_content" 
    android:text="Virbate"></Button> 

<Button 
android:id="@+id/Btn2" 
android:layout_width="30dp" 
android:text="Warm" 
android:textColor="#ffffff" 
android:paddingTop="10dp" 
android:background="#000000" 
android:layout_height="wrap_content" 
android:layout_weight="0.18"/> 

<Button 
android:id="@+id/Btn2" 
android:layout_width="40dp" 
android:text="Fresh" 
android:textColor="#ffffff" 
android:paddingTop="10dp" 
android:background="#000000" 
android:layout_height="wrap_content" 
android:layout_weight="0.18"/> 

<Button 
android:id="@+id/Btn2" 
android:layout_width="40dp" 
android:text="Calm" 
android:textColor="#ffffff" 
android:paddingTop="10dp" 
android:background="#000000" 
android:layout_height="wrap_content" 
android:layout_weight="0.09"/> 

</LinearLayout> 
</LinearLayout> 

<ScrollView 
android:id="@+id/ScrollView01" 
android:layout_width="fill_parent" 
android:scrollbars="vertical" 
android:layout_height="fill_parent"> 

    <HorizontalScrollView android:id="@+id/horizon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

<LinearLayout android:id="@+id/list_parent" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" 
    android:weightSum="1"> 


</LinearLayout> 
</HorizontalScrollView> 
</ScrollView> 

</LinearLayout> 

основной код класса идентификатор

public class DuluxColorGridActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 




} 

}

что я буду делать, чтобы интегрировать обе раскладки вместе, я хотите интегрировать динамический макет в макет main.xml в поле linearLayout id = list_parent ниже, я объявляю. помогите мне решить эту проблему.

<LinearLayout android:id="@+id/list_parent" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal" 
    android:weightSum="1"> 


</LinearLayout> 

ответ

0

Чтобы добавить настраиваемое представление в любой XML вам необходимо предоставить абсолютный путь класса (packagename+ classnaem) в XML

см Custom Composite Android Component

Пример Для создания пользовательских TableLayout

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.TableLayout; 

public class CustomTableLayout extends TableLayout{ 

    public CustomTableLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // Write your own code here 
    } 

} 

Для добавить абстракцию в xml, вы можете использовать следующий код:

<com.sunil.list.item.CustomTableLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     > 
+0

Спасибо Сунил Кумар Sahoo, но в этом макете linearlayout для столбца не добавляется, он показывает ошибку для создания конструктора для linearlayout. Как я могу решить эту проблему. пожалуйста помогите –

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