如何左对齐TableRow对象?

时间:2016-06-23 03:32:23

标签: android android-layout android-fragments

他们目前出现在中心。

enter image description here

Android Java代码

public void populateTable() {
    for(DoctorBean post: beanPostArrayList){
        String result = "";

        TableRow row = new TableRow(getActivity());
        row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        ImageView docImageView = new ImageView(getActivity());
        docImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        docImageView.setPadding(5, 5, 5, 5);
        docImageView.setImageResource(R.drawable.ic_doctor);

        TextView textView = new TextView(getActivity());
        textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        textView.setPadding(5, 5, 5, 5);

        result += "\n" + post.getHeroName();

        textView.setGravity(Gravity.LEFT);
        docImageView.setForegroundGravity(Gravity.LEFT);
        row.setGravity(Gravity.LEFT);

        textView.setText(result);
        row.addView(docImageView);
        row.addView(textView);
        tableLayout.addView(row);

    }
}

布局代码

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="229dp"
    class="com.google.android.gms.maps.SupportMapFragment" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:shrinkColumns="*"
        android:stretchColumns="*">

    </TableLayout>

</ScrollView></LinearLayout>

所以我想将ImageView和TextView对齐到左边。

1 个答案:

答案 0 :(得分:1)

另一种方法应该解决您的问题

  1. 您无需为TableView
  2. 添加显式滚动条
  3. TableView可以直接添加到您的主LinearLayout。正如您已指定MapView
  4. 的高度

    建议修改的xml

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mainLL"
        android:orientation="vertical">
    
        <com.google.android.gms.maps.MapView
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="229dp"
            class="com.google.android.gms.maps.SupportMapFragment" />
        <TableLayout
                android:id="@+id/tableLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
        </TableLayout>
        </LinearLayout>
    

    相应的代码更改

             LinearLayout linearLayout=(LinearLayout)view.findViewById(R.id.mainLL);
    
            //Table Layout parameters
            TableRow.LayoutParams textViewParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f);
            TableLayout tableLayout = (TableLayout) view.findViewById(R.id.tableLayout);
            TableRow trHead = new TableRow(context);
            LayoutParams tableRowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            trHead.setLayoutParams(tableRowParams);
    
            TextView nameHead = new TextView(context);
            nameHead.setText("Content left");
            nameHead.setLayoutParams(textViewParam);
            nameHead.setGravity(Gravity.LEFT);//or Gravity.CENTER according to your requirement
            trHead.addView(nameHead);
    
            TextView detailHead = new TextView(context);
            detailHead.setText("Content right");
            detailHead.setGravity(Gravity.RIGHT);//or Gravity.CENTER according to your requirement
            detailHead.setLayoutParams(textViewParam);
    
            trHead.addView(detailHead);
            tableLayout.addView(trHead);
    
            //add table layout to linear layout
    
            linearLayout.add(tableLayout);
    

    注意:

      

    许多值都来自资源文件,您也可以   忽视/复制那些。我添加了两个textview。你可能需要   根据您的要求改变

相关问题