当前位置: 代码迷 >> Android >> 如何在android中显示textview左对齐
  详细解决方案

如何在android中显示textview左对齐

热度:40   发布时间:2023-08-04 10:09:11.0

在我的应用程序中,我想将 VIEW ALL 显示为左对齐,如下图所示:-

但是我发现了这种类型的布局,我也以编程方式完成所有这些布局。

我的源代码:-

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);
    }
}

我是android编程的新手。 请帮我!

如果您还没有找到解决方案,那么您可以尝试以下操作
textView.setGravity(Gravity.RIGHT); 这基本上充当 TextView 中文本的重力以获取更多信息, 因此基本上它是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 分配给 LinearLayout 并将 weight=1 分配给每个 TextView。

解决方案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); 是可选的。

根据评论更新
如果您想要下面 SAMPLE 中的带下划线的文本,请在查看答案之一

 <u>Sample</u>

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

  相关解决方案