2017-07-27 2 views
1

J'ai créé une vue de tableau en utilisant la disposition de table et la rangée de table, et puis je suis confronté au problème de taille, je veux que la taille soit égale à la taille table. Je le crée avec TableRow dynamiquement, voici mon code:Android - Comment faire pour que la taille de la ligne de table soit égale à

TableLayout xml.

<android.widget.HorizontalScrollView 
    android:id="@+id/hv_cust_prop" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scrollbarAlwaysDrawHorizontalTrack="false"> 

    <TableLayout 
     android:id="@+id/table_content" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:measureWithLargestChild="true" 
     android:stretchColumns="*" 
     android:weightSum="1" /> 

</android.widget.HorizontalScrollView> 

Élément TableRow xml.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="fill_horizontal|fill_vertical" 
    android:gravity="fill_horizontal|center_vertical" 
    android:orientation="vertical"> 

    <FrameLayout 
     android:id="@+id/container_header" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="fill_horizontal|fill_vertical" 
     android:background="@drawable/cell_header_bg"> 

     <TextView 
      android:id="@+id/txt_header" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="fill_horizontal|fill_vertical" 
      android:layout_margin="2dp" 
      android:gravity="center" 
      android:maxWidth="150dp" 
      android:padding="7dp" 
      android:text="Header 1" 
      android:textStyle="bold" /> 
    </FrameLayout> 
</LinearLayout> 

Ligne génératrice Java.

row = new TableRow(getActivity()); 
         TableRow.LayoutParams lp = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1); 
         lp.gravity = Gravity.FILL; 
         row.setLayoutParams(lp); 
         View head = LayoutInflater.from(getActivity()).inflate(R.layout.item_cell_cust_prop, null); 

         FrameLayout container = (FrameLayout) head.findViewById(R.id.container_header); 
         container.setBackgroundResource(R.drawable.cell_bg); 

         TextView txtHeader = (TextView) head.findViewById(R.id.txt_header); 
         try { 
          txtHeader.setText(object.getString("CV_VALUE")); 
         } catch (JSONException e) { 
          txtHeader.setText("erorr"); 
          e.printStackTrace(); 
         } 

         row.addView(head); 
         tableContent.addView(row); 

avec tableContent est TableLayout du précédent xml.

**And this is the result**

Je veux une hauteur de la colonne suit la plus grande hauteur identique à la colonne avec le texte enroulé.

Toute aide sera appréciée. Merci,

Répondre

0

ajouter une seule ligne vraie (ce qui est depricated) ou MaXLine 1 à textview comme ci-dessus

<TextView 
      android:id="@+id/txt_header" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="fill_horizontal|fill_vertical" 
      android:layout_margin="2dp" 
      android:gravity="center" 
      android:maxWidth="150dp" 
      android:padding="7dp" 
      android:text="Header 1" 
      android:textStyle="bold" 
      android:maxLines="1" 
      android:singleLine="true" /> 

Modifier

Vous pouvez également essayer de mettre android: weightSum = "1" LinearLayout et définir android: layout_weight = "1" à FrameLayout comme ci-dessous

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_gravity="fill_horizontal|fill_vertical" 
android:gravity="fill_horizontal|center_vertical" 
android:orientation="vertical" 
android:weightSum="1"> 

<FrameLayout 
    android:id="@+id/container_header" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="fill_horizontal|fill_vertical" 
    android:background="@drawable/cell_header_bg" 
    android:layout_weight="1"> 

    <TextView 
     android:id="@+id/txt_header" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="fill_horizontal|fill_vertical" 
     android:layout_margin="2dp" 
     android:gravity="center" 
     android:maxWidth="150dp" 
     android:padding="7dp" 
     android:text="Header 1" 
     android:textStyle="bold" /> 
</FrameLayout> 

+0

hi @Omi merci, mais je veux que mon textview ait la largeur maximale, donc, il sera enveloppé si le texte dépasse la largeur maximale. :( mais, merci pour la réponse. :) –

+0

bonjour @yascodwiy essayer le code de mise à jour – Omi