[英]Alignment of imageview in tablelayout
This should be easy, how can I align the star in the middle.这应该很容易,我怎样才能将星星对准中间。 I'm adding the top header textviews and stars dynamically in code.我正在代码中动态添加顶部标题文本视图和星号。 I tried setting gravity on the imageview did not work.我尝试在图像视图上设置重力不起作用。
UPDATE: I added iv.setScaleType(ImageView.ScaleType.FIT_START)
now star aligns to left更新:我添加了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);
}
You should do this:你应该做这个:
TableRow.LayoutParams params = new TableRow.LayoutParams(0,WRAP_CONTENT);
you should have layout params of tablerow for textviews.你应该有 textviews 的 tablerow 布局参数。
then in this params you should add weight.那么在这个参数中你应该增加权重。
params.weight = 1;
params.gravity = GRAVITY.CENTER;
and your table row should have gravity "Center" for its content (not layout gravity).并且您的表格行应该具有其内容的重力“中心”(而不是布局重力)。
Try do this:尝试这样做:
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);
}
In this code i have created new layout params as params2 because the textview was using weight of 1 so that your images were little right sided.在此代码中,我创建了新的布局参数作为 params2,因为 textview 使用的权重为 1,因此您的图像很少位于右侧。 i had used .8f weight for the names so now its working ok.我曾使用 .8f 重量作为名称,所以现在它工作正常。 so do as per your req.所以按照你的要求做。 Hope it Helps!!希望能帮助到你!!
Here's a minimal solution based on the above comments:这是基于上述评论的最小解决方案:
<ImageView
android:scaleType="fitCenter"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/cross" />
The first half containes the attributes that seem to be necessary to solve the problem.前半部分包含似乎解决问题所必需的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.