开发者

android layout dynamic sizing

For many "header" TextViews across the pages I'm designing for a program, I would like them to be the parent.Width / 2 then properly aligned. While that would be fairly easy to code in the Java, I am attempting to do as much as possible in the XML layouts to avoid XML-Java code intersections until the last little bits (button presses, finish page, etc).

Do I have to go through each page and calculate every item's specific width myself or is there a way to put something along the lines of "fill_parent 开发者_如何学Python/ 2"?

EDIT: Forgot to mention what is likely a key note - almost everything I am doing is in RelativeLayouts, I have very little use for LinearLayouts in this project.


If you have a LinearLayout that is flush to the left and right, you can do the following:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:gravity="left"
    android:orientation="horizontal">
    <TextView
        android:layout_weight="1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:text="I take up half the width!!!1" />
</LinearLayout>

By setting the weightSum on the parent, you're saying that the weights of the children should equal that amount. By setting the single child's weight to half of that, it'll take up half the space. Make sure to set the width of the child to 0 so it knows to use the weight value to calculate its space relative to its parent.

Then, you can align it however you'd like by setting gravity on the parent LinearLayout.


Use a tableview with two columns where each column is stretched and has a text view. Then hide the second textview

<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1">
  <TableRow>
    <TextView android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" Set Item Name "/>
    <TextView android:id="@+id/hiddenTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="invisible"/>
  </TableRow>
</TableLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜