【发布时间】:2015-05-01 04:09:38
【问题描述】:
我有一个包含 3 列的 TableLayout,第一列中的文本要大得多。我想要的是文本视图与 TableRow 的顶部对齐。我必须以编程方式进行,因为表是动态的,所以它也是在 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;
}
}
我该怎么做?
提前致谢。
【问题讨论】:
-
请发布您的 xml/代码
标签: android text textview alignment tablelayout