簡體   English   中英

Android TableLayout重力和填充問題

[英]Android TableLayout gravity and padding issue

好吧,所以我目前正在使用TableLayout進行活動的布局,並將該死的表放到我想要的位置,實際上並不能很好地解決問題。

這是現在的屏幕截圖: 在此處輸入圖片說明

我目前有三個目標:

  1. 使包含EditText和ToggleButton的LinearLayout在屏幕中水平居中(但也無法做到這一點)

  2. 將表格布局(上面有ftn的6個按鈕)水平放置在LL下方水平居中, 而不將其分成3個LinearLayouts

  3. 使按鈕全部具有相同的大小,除了邊距和填充以外,不使用絕對值。

由於我已經基本耗盡了自己的xml布局知識,並且寧願避免以編程方式進行此類操作(我通常在這種情況下也會這樣做),因此對我將如何實現上述所有或所有目標的任何見解將不勝感激。

作為參考,下面是上面看到當前布局的XML(XML版本線存在 ,但它只是不希望在這個崗位合適的復制):

<com.hinodesoftworks.lordkat.widgets.TitleBar
    android:id="@+id/calc_titlebar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/calc_input_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" >

    <EditText
        android:id="@+id/calc_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/txt_trig_hint" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:background="@drawable/button_selector"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:textOff="@string/txt_trig_toggle_radians"
        android:textOn="@string/txt_trig_toggle_degrees" />
</LinearLayout>

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:weightSum="3" >

    <TableRow
        android:layout_gravity="center_horizontal"
        android:layout_weight="1" >

        <Button
            android:id="@+id/trig_sine_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="1dp"
            android:layout_marginTop="2dp"
            android:background="@drawable/button_selector"
            android:paddingBottom="10dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:text="@string/txt_trig_sine_button" />

        <Button
            android:id="@+id/trig_secant_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="1dp"
            android:layout_marginTop="2dp"
            android:background="@drawable/button_selector"
            android:paddingBottom="10dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:text="@string/txt_trig_secant_button" />
    </TableRow>

    <TableRow
        android:layout_gravity="center_horizontal"
        android:layout_weight="1" >

        <Button
            android:id="@+id/trig_cosine_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="1dp"
            android:layout_marginTop="2dp"
            android:background="@drawable/button_selector"
            android:paddingBottom="10dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:text="@string/txt_trig_cosine_button" />

        <Button
            android:id="@+id/trig_cosecant_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="1dp"
            android:layout_marginTop="2dp"
            android:background="@drawable/button_selector"
            android:paddingBottom="10dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:text="@string/txt_trig_cosecant_button" />
    </TableRow>

    <TableRow
        android:layout_gravity="center_horizontal"
        android:layout_weight="1" >

        <Button
            android:id="@+id/trig_tangent_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="1dp"
            android:layout_marginTop="2dp"
            android:background="@drawable/button_selector"
            android:paddingBottom="10dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:text="@string/txt_trig_tangent_button" />

        <Button
            android:id="@+id/trig_cotangent_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="1dp"
            android:layout_marginTop="2dp"
            android:background="@drawable/button_selector"
            android:paddingBottom="10dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:text="@string/txt_trig_cotangent_button" />
    </TableRow>
</TableLayout>

<TextView
    android:id="@+id/calc_results_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/txt_trig_results_label" />

<TextView
    android:id="@+id/calc_results"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="123.456" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

1-)要使EditText和ToggleButton在屏幕中水平居中,請使用android:gravity =“ center_horizo​​ntal”
而不是“ android:layout_gravity =“ center_horizo​​ntal”

2-)與答案(1)一樣,要使表格水平居中,請使用“ android:gravity =“ center_horizo​​ntal”

3-)要使按鈕的大小全部相同,並且不使用除邊距和填充之外的絕對值,請使用“ android:stretchColumns =” 0,1“”來拉伸列0和1。使用此屬性時,不必使用絕對值。 因此,結果代碼如下:

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:stretchColumns="0,1"
   >

<TableRow>
    <Button
        android:id="@+id/trig_sine_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"
        android:text="txt_trig_sine_button" />
    <Button
        android:id="@+id/trig_secant_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"
        android:text="txt_trig_secant_button" />
</TableRow>

....

嘗試使用GridLayout,它具有更高的內存效率,沒有嵌套容器,並且非常適合此類網格狀設計。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM