Android TextView 在 TableLayout 内部对齐

Android TextView align inside of TableLayout

我有一个包含 3 列的 TableLayout,第一列中的文本要大得多。我想要的是文本视图与 TableRow 的顶部对齐。我必须以编程方式进行,因为 table 是动态的,所以它也在 Java 文件中生成。

我的java:

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;

public class B10o extends SherlockFragment {

String[] column1;
String[] column2;
String[] column3;
String[] textView1;
String[] textView2;
Intent intent;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Get the view from fragmenttab1.xml
    View view = inflater.inflate(R.layout.b10o, container, false);

    intent = new Intent(getActivity(), B10so.class);
    final Button button = (Button) view.findViewById(R.id.button1);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(intent);
        }
    });

    TableLayout prices = (TableLayout)view.findViewById(R.id.table);
    String[] column1 = getResources().getStringArray(R.array.b10oo);
    String[] column2 = getResources().getStringArray(R.array.b10op1);
    String[] column3 = getResources().getStringArray(R.array.b10op2);
    String[] textView1 = getResources().getStringArray(R.array.title);
    String[] textView2 = getResources().getStringArray(R.array.subtitle);
    prices.setStretchAllColumns(true);
    prices.bringToFront();
    for(int i = 0; i < column1.length; i++){
        TableRow tr =  new TableRow(getActivity());
        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT));
        TextView c1 = new TextView(getActivity());
        c1.setText(column1[i]);
        c1.setTypeface(null, Typeface.BOLD);
        c1.setTextSize(40);
        c1.setPadding(0, 0, 20, 0);
        TextView c2 = new TextView(getActivity());
        c2.setText(column2[i]);
        TextView c3 = new TextView(getActivity());
        c3.setText(column3[i]);
        c3.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        c3.setGravity(Gravity.TOP);
        tr.addView(c1);
        tr.addView(c2);
        tr.addView(c3);
        prices.addView(tr);

    }
    TextView textView1t = (TextView) view.findViewById(R.id.textView1);
    textView1t.setText(textView1[0]);
    TextView textView2t = (TextView) view.findViewById(R.id.textView2);
    textView2t.setText(textView2[0]);
    return view;
}

}

我该怎么做?

提前致谢。

可以通过为最后两个文本视图设置android:layout_gravity="center_vertical"来完成。以下代码可以满足您的要求。

    <?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" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="4"
                android:textSize="30dp" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:text="100,25" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:text="35" />
        </TableRow>
    </TableLayout>

</LinearLayout>

可以使用 setGravity() 来完成。 代码将如下所示。

TextView c1 = new TextView(this);
c1.setGravity(Gravity.CENTER_VERTICAL);

希望这就是您要找的!