简体   繁体   English

android TextView 以编程方式布局

[英]android TextView Layout programmatically

I want to align the TextViews one on the right the other on the left and each row contain one text view... I don't wanna use Width:fill_parent setGravity(Gravity.Right) coz I am using a background.我想将 TextViews 一个在右边对齐,另一个在左边对齐,每一行都包含一个文本视图......我不想使用Width:fill_parent setGravity(Gravity.Right)因为我正在使用背景。 here's my code:这是我的代码:

LinearLayout mainChatLayout = new LinearLayout(main.this);
//ChatText.setId((int)System.currentTimeMillis());
lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
((android.widget.RelativeLayout.LayoutParams) lp).addRule(RelativeLayout.BELOW, recent.getId());
tv = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//ChatText.setBackgroundColor(0xfff00000);#800080
if(recent.getId() % 2 == 0) {
    ChatText = new TextView(main.this);
    mainChatLayout.setId((int)System.currentTimeMillis());
    ChatText.setBackgroundDrawable(getResources().getDrawable(R.drawable.talk1));
    ChatText.setGravity(Gravity.RIGHT); // not working 
    ChatText.setText("some text");
    ChatText.setTextColor(Color.parseColor("#333333"));
    ChatText.setPadding(0, 10, 20, 10);
}
else{
    ChatText = new TextView(main.this);
    mainChatLayout.setId((int)System.currentTimeMillis());
    ChatText.setBackgroundDrawable(getResources().getDrawable(R.drawable.talk1));
    ChatText.setGravity(Gravity.LEFT);
    ChatText.setText("Time: " + System.currentTimeMillis());
    ChatText.setTextColor(Color.parseColor("#333333"));
    ChatText.setPadding(20, 10, 0, 10);
}
mainChatLayout.addView(ChatText, tv);
MainLayout.addView(mainChatLayout, lp);
recent = mainChatLayout;
            LinearLayout ll= /// Find Your Layout Using ID
            TextView text=new TextView(this);

Now set Structure using LayoutParam.现在使用 LayoutParam 设置结构。 LayoutParam will be type of Parent of TextView LayoutParam 将是 TextView 的父类型

    text.setLayoutParams(new LinearLayout.LayoutParams(100,100));
    ll.addView(text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM