Android TextView在TableLayout内部对齐

时间:2015-02-28 13:18:44

标签: android text textview alignment tablelayout

我有一个包含3列的TableLayout,第一列中的文字要大得多。我想要的是textviews与TableRow的顶部对齐。我必须以编程方式执行此操作,因为该表是动态的,因此它也是在Java文件中生成的。

enter image description here

我的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;
}

}

我该怎么做?

提前致谢。

2 个答案:

答案 0 :(得分:1)

可以使用setGravity()来完成。 代码看起来像这样。

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

希望这就是你要找的东西!

答案 1 :(得分:0)

可以通过为最后两个textviews设置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>