网格布局改变宽度
问题描述:
我一直在试图让一个GridLayout的使用工作:网格布局改变宽度
<GridLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<TextView
android:text="sdfgh"
android:layout_height="wrap_content"
android:id="@+id/AvgGrade"
android:layout_row="2"
android:layout_column="1"
android:layout_rowWeight="0.2"
android:layout_columnWeight="0.5"
android:layout_gravity="fill"
android:textAlignment="gravity"
android:layout_columnSpan="1" />
<TextView
android:text="words"
android:layout_height="wrap_content"
android:id="@+id/Totals"
android:layout_row="1"
android:layout_column="1"
android:textAlignment="gravity"
android:layout_rowWeight="0.2"
android:layout_columnWeight="0.5"
android:layout_gravity="fill"
android:layout_columnSpan="1" />
<TextView
android:text="words"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:layout_row="2"
android:layout_column="0"
android:textAlignment="center"
android:layout_rowWeight="0.2"
android:layout_columnWeight="0.5"
android:layout_gravity="fill"
android:layout_columnSpan="1" />
<TextView
android:text="words"
android:layout_height="wrap_content"
android:id="@+id/textView4"
android:layout_row="1"
android:layout_column="0"
android:layout_rowWeight="0.2"
android:layout_columnWeight="0.5"
android:layout_gravity="fill"
android:textAlignment="center"
android:layout_columnSpan="1" />
<TextView
android:text="words"
android:layout_height="wrap_content"
android:id="@+id/TotalAvg"
android:layout_row="0"
android:layout_column="1"
android:textAlignment="gravity"
android:layout_rowWeight="0.2"
android:layout_columnWeight="0.5"
android:layout_gravity="fill"
android:layout_columnSpan="1" />
<TextView
android:text="words"
android:layout_height="wrap_content"
android:id="@+id/textView6"
android:layout_row="0"
android:layout_column="0"
android:textAlignment="center"
android:layout_columnWeight="0.5"
android:layout_gravity="fill"
android:layout_columnSpan="1" />
</GridLayout>
我的问题是,什么样的数据是负载在第二列列的大小发生了改变,所以咯。我已经尝试了左侧和*对齐,并没有什么似乎停止改变列。用户将重复更新这些值,并且列的轻微“微动”移动看起来非常糟糕。
答
经过很多挫折之后,我无法在XML布局中找到解决方案。不过,我确实找到了一个解决方案,似乎在编程上运行得很好。
我添加了一个名为在年底方法我在OnCreate
public void ResizeTexts(){
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpWidth = (outMetrics.widthPixels/density);
text1.getLayoutParams().width = (int)dpWidth;
text2.getLayoutParams().width = (int)dpWidth;
text3.getLayoutParams().width = (int)dpWidth;
text4.getLayoutParams().width = (int)dpWidth;
text5.getLayoutParams().width = (int)dpWidth;
text6.getLayoutParams().width = (int)dpWidth;
}
我适应从这个链接re-setting a TextView height programmatically
然后在我的XML布局,我设置了:
<GridLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<TextView
android:text="points"
android:layout_height="wrap_content"
android:layout_width="10dp"
android:id="@+id/text1"
android:layout_row="0"
android:layout_column="0"
android:textAlignment="textEnd"
/>
<TextView
android:text="totals"
android:layout_height="wrap_content"
android:layout_width="10dp"
android:id="@+id/text2"
android:layout_row="1"
android:layout_column="0"
android:textAlignment="textEnd"/>
<TextView
android:text="score"
android:layout_height="wrap_content"
android:id="@+id/text3"
android:layout_width="10dp"
android:layout_row="2"
android:layout_column="0"
android:textAlignment="textEnd"/>
<TextView
android:text="123.4"
android:layout_height="wrap_content"
android:id="@+id/text4"
android:layout_width="10dp"
android:layout_row="1"
android:layout_column="1"
android:textAlignment="textEnd"/>
<TextView
android:text="1.23"
android:layout_height="wrap_content"
android:id="@+id/text5"
android:layout_width="10dp"
android:layout_row="2"
android:layout_column="1"
android:textAlignment="textEnd"/>
<TextView
android:text="1234 "
android:layout_height="wrap_content"
android:id="@+id/text6"
android:layout_width="10dp"
android:layout_row="0"
android:layout_column="1"
android:textAlignment="textEnd"/>
</GridLayout>
*我正在使用xml布局中的布局宽度来尝试“破坏”它,但实际上并未使用它。
无可否认,我不确定这是否是“最好”的方式来做到这一点。我个人觉得GridLayout XML 应该能够自己做到这一点。但是,我无法弄清楚。