0

这应该很容易,我怎样才能将星星对准中间。我正在代码中动态添加顶部标题文本视图和星号。我尝试在图像视图上设置重力不起作用。

更新:我现在添加iv.setScaleType(ImageView.ScaleType.FIT_START)了星号向左对齐

在此处输入图像描述 在此处输入图像描述

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
        android:layout_height="fill_parent" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
   <TableLayout  
    android:id="@+id/TableAch"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:background="@color/white"
    android:layout_alignParentTop="true"
    android:shrinkColumns="*"
    android:stretchColumns="*"
    >

        <TableRow  
        android:id="@+id/RowAchTitle"  
        android:layout_height="wrap_content"  
        android:layout_width="match_parent"  
        android:gravity="center_horizontal"
        >  

            <TextView  
                android:id="@+id/ViewAchTitle"  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                android:textStyle="bold"  
                android:text="Achievements"  
                android:gravity="center"  
                android:layout_span="6">
            </TextView>  

        </TableRow>  
       <TableRow  
        android:id="@+id/RowAchHeader"  
        android:layout_height="wrap_content"  
        android:layout_width="match_parent"
        android:weightSum="6"
        >   
        </TableRow>

    </TableLayout>



    </RelativeLayout>

@Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.acheivements);

            tbLayout = (TableLayout) this.findViewById(R.id.TableAch);
            rowHeader = (TableRow) this.findViewById(R.id.RowAchHeader);

            FlurryAgent.logEvent("Achievements"); 

            TableRow.LayoutParams tableRowParams=
                      new TableRow.LayoutParams
                      (TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);

            tableRowParams.setMargins(0, 0, 0, 5);


            TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT);

            params.weight = 1;
            params.gravity= Gravity.CENTER;

            int fontSize = 12;

            TextView tv = new TextView(this);
            tv.setLayoutParams(params);
            tv.setText("     ");
            tv.setTextSize(fontSize);
            rowHeader.addView(tv); //This is blank top left corner

            for (Level level : Level.values()) { //Then we add all the headers for games
                tv = new TextView(this);

                tv.setText("EASY");
                tv.setTextSize(fontSize);
                tv.setLayoutParams(params);
                rowHeader.addView(tv);
            }

            //Next we add the levels for each game, putting a pass image where passed



            for (Games  game : Games.values()) {
                TableRow newRow = new TableRow(this);
                newRow.setLayoutParams(tableRowParams);

                String[] words = game.name().split("(?=[A-Z])");
                String name = "";
                for (String word : words) {
                    if(word.length() < 1) continue;
                    name += word + "\n";
                }

                tv = new TextView(this);
                tv.setTextSize(fontSize);
                tv.setText(name);
                tv.setLayoutParams(params);
                newRow.addView(tv);
                newRow.setWeightSum(6);

                for (Level level : Level.values()) {
                    ImageView iv = new ImageView(this);

                        iv.setScaleType(ImageView.ScaleType.FIT_START);
                    iv.setLayoutParams(params);

                    tv = new TextView(this);
                    tv.setLayoutParams(params);
                    tv.setText("EASY");
                    tv.setTextSize(fontSize);

                    if( acheivements.getPassed(level, game) )//has achievement
                    {
                        iv.setImageResource(R.drawable.star_gold_full_small);
                    }
                    else {
                        iv.setImageResource(R.drawable.star_gold_empty_small);
                    }
                    newRow.addView(tv);
                }
                tbLayout.addView(newRow);
            }

            setupAds(true);
     }
4

1 回答 1

2

你应该做这个:

TableRow.LayoutParams params = new TableRow.LayoutParams(0,WRAP_CONTENT);

你应该有 textviews 的 tablerow 布局参数。

那么在这个参数中你应该增加权重。

params.weight = 1;
params.gravity = GRAVITY.CENTER;

并且您的表格行应该具有其内容的重力“中心”(而不是布局重力)。

尝试这样做:

for (Games  game : Games.values()) {
                TableRow newRow = new TableRow(this);
                newRow.setLayoutParams(tableRowParams);

TableRow.LayoutParams params2 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT);

                params2.weight = .8f;
                params2.gravity= Gravity.CENTER;

                String[] words = game.name().split("(?=[A-Z])");
                String name = "";
                for (String word : words) {
                    if(word.length() < 1) continue;
                    name += word + "\n";
                }

                tv = new TextView(this);
                tv.setTextSize(fontSize);
                tv.setText(name);
                tv.setLayoutParams(params2);
                newRow.addView(tv);
                newRow.setWeightSum(6);

                for (Level level : Level.values()) {
                    ImageView iv = new ImageView(this);

                    iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                    iv.setAdjustViewBounds(true);
                    iv.setLayoutParams(params);

                    tv = new TextView(this);
                    tv.setLayoutParams(params);
                    tv.setText("EASY");
                    tv.setTextSize(fontSize);

                    if( acheivements.getPassed(level, game) )//has achievement
                    {
                        iv.setImageResource(R.drawable.star_gold_full_small);
                    }
                    else {
                        iv.setImageResource(R.drawable.star_gold_empty_small);
                    }
                    newRow.addView(tv);
                }
                tbLayout.addView(newRow);
            }

在此代码中,我创建了新的布局参数作为 params2,因为 textview 使用的权重为 1,因此您的图像很少位于右侧。我曾使用 .8f 重量作为名称,所以现在它工作正常。所以按照你的要求做。希望能帮助到你!!

于 2013-08-21T09:39:55.700 回答