tablelayout中imageview的对齐方式

时间:2013-08-21 08:49:21

标签: java android alignment android-imageview tablelayout

这应该很简单,如何在中间对齐星星。我在代码中动态添加顶部标题文本视图和星标。我尝试在imageview上设置重力不起作用。

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

enter image description here enter image description here

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

1 个答案:

答案 0 :(得分:2)

你应该这样做:

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

你应该为textrow设置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重量的名称所以现在它的工作正常。按照你的要求做。 希望它能帮助!!