2015-10-19 92 views
-1

在我的应用我想所有显示VIEW左对齐左对齐像下图: - enter image description here如何显示TextView的android系统

但是我发现这种类型的布局和更多的事情我也做所有的这些布局编程。 enter image description here

我的源码: -

private void printFrontCategory(){ 

    for(int i=0;i<main.size();i++) { 

     View v = new View(MainActivity.this); 
     v.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT, 5)); 
     v.setBackgroundColor(Color.rgb(51, 51, 51)); 

    /* View v1 = new View(MainActivity.this); 
     v1.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT,5)); 
     v1.setBackgroundColor(Color.rgb(255, 255, 255));*/ 

     HorizontalScrollView horizontalScrollView = new HorizontalScrollView(MainActivity.this); 
     horizontalScrollView.setHorizontalScrollBarEnabled(false); 

     TextView textView = new TextView(MainActivity.this); 
     String content = front_category_catetory_name.get(i); 
     content = content.replace("&#039;", ""); 
     content = content.replace("&amp;",""); 
     textView.setText(content); 
     textView.setGravity(Gravity.RIGHT); 
     // textView.setGravity(Gravity.CENTER); 
     textView.setTextSize(20); 

     TextView textView2 = new TextView(MainActivity.this); 
     textView2.setText("VIEW ALL"); 
     textView2.setTextColor(Color.BLUE); 
     textView2.setGravity(Gravity.LEFT); 

     LinearLayout linearLayout2 = new LinearLayout(MainActivity.this); 
     linearLayout2.setOrientation(LinearLayout.HORIZONTAL); 

     linearLayout2.addView(textView,0); 
     linearLayout2.addView(textView2,1); 


     LinearLayout linearLayout = new LinearLayout(MainActivity.this); 
     linearLayout.setOrientation(LinearLayout.HORIZONTAL); 

     for (int j = 0; j < main.get(i).size(); j++) { 
      LinearLayout linearLayout1 = new LinearLayout(MainActivity.this); 
      linearLayout1.setOrientation(LinearLayout.VERTICAL); 

      ImageView image = new ImageView(MainActivity.this); 
      TextView nameProduct = new TextView(MainActivity.this); 
      TextView priceProduct = new TextView(MainActivity.this); 
      TextView special_discount = new TextView(MainActivity.this); 
       /* Log.d("counter val",cnt+""); 
        Log.d("thumb ",front_category_thumb.size()+""); 
        Log.d("image", front_category_thumb.get(52));*/ 
      new SetImageView(image).execute(main.get(i).get(j).get(1)); 

      nameProduct.setText(main.get(i).get(j).get(2)); 

      if (!String.valueOf(main.get(i).get(j).get(5)).equals(null)) { 
       priceProduct.setText(main.get(i).get(j).get(3)); 
       special_discount.setText(main.get(i).get(j).get(5)); 
       priceProduct.setPaintFlags(nameProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
       priceProduct.setGravity(Gravity.CENTER); 
       special_discount.setGravity(Gravity.CENTER); 
       nameProduct.setGravity(Gravity.CENTER); 
       linearLayout1.addView(image); 
       linearLayout1.addView(nameProduct); 
       linearLayout1.addView(priceProduct); 
       linearLayout1.addView(special_discount); 
      } else if (!String.valueOf(main.get(i).get(j).get(4)).equals(null)) { 
       priceProduct.setText(main.get(i).get(j).get(3)); 
       special_discount.setText(main.get(i).get(j).get(4)); 
       priceProduct.setPaintFlags(nameProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 
       priceProduct.setGravity(Gravity.CENTER); 
       special_discount.setGravity(Gravity.CENTER); 
       nameProduct.setGravity(Gravity.CENTER); 
       linearLayout1.addView(image); 
       linearLayout1.addView(nameProduct); 
       linearLayout1.addView(priceProduct); 
       linearLayout1.addView(special_discount); 
      } else { 
       priceProduct.setText(main.get(i).get(j).get(3)); 
       priceProduct.setGravity(Gravity.CENTER); 
       nameProduct.setGravity(Gravity.CENTER); 
       linearLayout1.addView(image); 
       linearLayout1.addView(nameProduct); 
       linearLayout1.addView(priceProduct); 
      } 
      linearLayout.addView(linearLayout1, j); 
     } 
     horizontalScrollView.addView(linearLayout); 
    // linearLayoutmens.addView(textView); 
    // linearLayoutmens.addView(v1); 
     linearLayoutmens.addView(linearLayout2); 
     linearLayoutmens.addView(horizontalScrollView); 
     linearLayoutmens.addView(v); 
    } 
} 

我在机器人编程新。请帮帮我!

+0

显示你的XML文件不是你的班级... – Aspicas

+0

我创建了我所有的布局组件动态 –

+0

你有你的'pricesProducts'具有中心重力,试图改变它,并动态创建所有布局,这不是最好的选择'内存泄漏,你需要更多的使用“CPU”,也许在较旧的设备上......这是非常糟糕的。像“OutOfMemory”这样的错误可能会出现。 – Aspicas

回答

1

如果你还没有找到解决办法还没有,那么你可以尝试以下
textView.setGravity(Gravity.RIGHT);这基本上充当重力内的TextView文本更多信息refer所以基本上它是android:Gravity计数器组成部分。 因此,我们需要LayoutParam
您的问题,它可以使用的LinearLayout和RelativeLayout的
变化下面的代码从

TextView textView = new TextView(MainActivity.this); 
String content = front_category_catetory_name.get(i); 
content = content.replace("&#039;", ""); 
content = content.replace("&amp;",""); 
textView.setText(content); 
textView.setGravity(Gravity.RIGHT); 
// textView.setGravity(Gravity.CENTER); 
textView.setTextSize(20); 

TextView textView2 = new TextView(MainActivity.this); 
textView2.setText("VIEW ALL"); 
textView2.setTextColor(Color.BLUE); 
textView2.setGravity(Gravity.LEFT); 

LinearLayout linearLayout2 = new LinearLayout(MainActivity.this); 
linearLayout2.setOrientation(LinearLayout.HORIZONTAL); 

linearLayout2.addView(textView,0); 
linearLayout2.addView(textView2,1); 

解决方法1使用的LinearLayout

TextView textView = new TextView(MainActivity.this); 
String content = front_category_catetory_name.get(i); 
content = content.replace("&#039;", ""); 
content = content.replace("&amp;",""); 
textView.setText(content); 
textView.setGravity(Gravity.START); //Gravity.Left also works 
textView.setTextSize(20); 

TextView textView2 = new TextView(MainActivity.this); 
textView2.setText("VIEW ALL"); 
textView2.setTextColor(Color.BLUE); 
textView2.setGravity(Gravity.END); //Gravity.Right also works 

LinearLayout linearLayout2 = new LinearLayout(MainActivity.this); 
linearLayout2.setOrientation(LinearLayout.HORIZONTAL); 

LinearLayout.LayoutParams linearLayoutParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT,2); 
linearLayout2.setLayoutParams(linearLayoutParam); 

LinearLayout.LayoutParams viewParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT,1); 

linearLayout2.addView(textView, 0, viewParam); 
linearLayout2.addView(textView2, 1, viewParam); 

在以上两种方式来解决解决方案我已将weightSum = 2分配给每个TextView的LinearLayout和weight = 1。

溶液2使用的RelativeLayout

TextView textView = new TextView(MainActivity.this); 
String content = front_category_catetory_name.get(i); 
content = content.replace("&#039;", ""); 
content = content.replace("&amp;",""); 
textView.setText(content); 
//textView.setGravity(Gravity.START); //Gravity.Left also works 
textView.setTextSize(20); 

TextView textView2 = new TextView(MainActivity.this); 
textView2.setText("VIEW ALL"); 
textView2.setTextColor(Color.BLUE); 
//textView2.setGravity(Gravity.END); //Gravity.Right also works 

RelativeLayout relativeLayout2 = new RelativeLayout(MainActivity.this); 

RelativeLayout.LayoutParams relativeLayoutParam1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
relativeLayoutParam1.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
relativeLayout2.addView(textView, 0, relativeLayoutParam1); 

RelativeLayout.LayoutParams relativeLayoutParam2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
relativeLayoutParam2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
relativeLayout2.addView(textView2, 1, relativeLayoutParam2); 

在这种情况下textView.setGravity(Gravity.RIGHT);是可选的。基于评论

更新如果你想带下划线的文本,以与试样下面再检查答案的一个here

<u>Sample</u>

但是,如果你想有一个黑线则是视图,您需要在添加linearlayout2或relativelayout2之后添加它,具体取决于主视图中的解决方案。

+0

如果我想在文本视图的下方画一条线我能完成 –

+0

在解决方案中,我考虑过ViewAll文本没有与右对齐的问题。而关于线....你需要它...在linearlayout2以下textview或低于ViewAll文本? –

+0

是正好在textview和viewAll文本下面 –