2010-10-06 46 views
12

Tôi đang phát triển một ứng dụng Android.Căn chỉnh các nút trong TableRow

Tôi có điều này: nút

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="200px" 
    android:orientation="vertical"> 
    <TextView 
     android:id="@+id/gameTitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:gravity="left" 
     android:layout_margin="5px" 
     android:text="aaaaa"/> 
    <TextView 
     android:id="@+id/gameDescription" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:gravity="left" 
     android:layout_margin="5px" 
     android:typeface="sans" 
     android:textSize="16sp" 
     android:textStyle="bold" 
     android:text="dddddd"/> 
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/ButtonsTable" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <Button 
       android:text="@string/yes" 
       android:id="@+id/playGame" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 
      <Button 
       android:text="@string/no" 
       android:id="@+id/noPlayGame" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 
     </TableRow> 
    </TableLayout> 
</LinearLayout> 

Tôi muốn đặt nút những môn chơi thể thao ở phía bên trái của trung tâm, và noPlayGame ở phía bên phải của trung tâm.

Bây giờ, cả hai xuất hiện được căn chỉnh bên trái của TableRow.

Tôi làm cách nào để thực hiện việc này?

Cảm ơn bạn.

EDIT: Tôi đã thêm bố cục hoàn chỉnh và các sửa đổi được đề xuất bởi Ashay.
2 EDIT:
CÁC GIẢI PHÁP
tôi đã thêm vào TableLayout sau: android:stretchColumns="0,1". Bây giờ các nút được căn giữa nhưng chúng lấp đầy toàn bộ cột của chúng !!!

+1

Có lẽ đáng để đăng câu trả lời của riêng bạn và đánh dấu câu trả lời là đã trả lời, để điều này không tiếp tục hiển thị trong danh sách câu hỏi chưa được trả lời. –

+1

Tôi không thích trả lời các câu hỏi của mình. – VansFannel

Trả lời

21

Tôi hy vọng điều này sẽ giúp:

<TableRow 
    android:id="@+id/tableRow1" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:layout_width="wrap_content"> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="@string/stringtxt1"></Button> 
    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:text="@string/stringtxt2" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center|center_horizontal"></Button> 
</TableRow> 
2

Hủy bỏ dòng này <TableRow android:layout_gravity="center"> Và cho android:fill_containt trong dòng của bảng cho cả chiều cao & rộng

edited trả lời: Vâng, tôi rất xin lỗi vì sự bài viết cuối cùng đánh vần sai lầm

Thông qua XML có vẻ như không thể đối với TableLayout. Bạn cần đặt giao diện người dùng này trong mã khi khởi tạo. Trong tệp java của bạn, bạn có thể đặt chúng bằng cách sử dụng bên dưới.

playGame.setWidth(ScreenWidth()/2-var); 
noPlayGame.setWidth(ScreenWidth()/2-var); 

đây var thể được sử dụng để thiết lập khoảng cách giữa 2 nút như 10 hoặc 30. Ngoài ra, xml của bạn sẽ nhận được sự thay đổi như sau:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/ButtonsTable" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:gravity="center"> 
      <Button 
       android:text="yes" 
       android:id="@+id/playGame" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center"/> 
      <Button 
       android:text="no" 
       android:id="@+id/noPlayGame" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center"/> 
     </TableRow> 
</TableLayout> 

Phương pháp ScreenWidth() có thể được sử dụng cho nhận được chiều rộng của màn hình thông qua các phương pháp này

private int ScreenWidth() { 
     DisplayMetrics dm=new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(dm); 
     String width=""+dm.widthPixels; 
     return Integer.parseInt(width); 
} 
+0

android: fill_containt? Tôi đã không tìm thấy "tài sản" này trên tài liệu của Android. – VansFannel

+0

Tôi đã chỉnh sửa câu hỏi để thêm chi tiết. Câu trả lời của bạn không hiệu quả với tôi. – VansFannel

2

Tôi đã thêm hai bản xem trước văn bản giả để căn chỉnh nút của tôi ở giữa.

<TableRow> 
    <TextView 
     android:text="" 
     android:layout_weight=".25" 
     android:layout_width="0dip"   
     /> 
    <Button 
     android:id="@+id/buttonSignIn" 
     android:text="Log In" 
     android:layout_height="wrap_content" 
     android:layout_weight=".50" 
     android:layout_width="0dip" 
     /> 
    <TextView 
     android:text="" 
     android:layout_weight=".25" 
     android:layout_width="0dip"   
    /> 
</TableRow> 
+2

là giải pháp duy nhất này? Tôi không nghĩ rằng đây là chính xác .... thêm văn bản trống để trung tâm một nút? – Kennethvr

9

tôi gửi mã của tôi, tôi hy vọng rằng sẽ giúp người dùng khác để giải quyết vấn đề của họ (nội dung trung tâm). Tôi muốn căn giữa hai nút trong hàng của bảng, đây là mã của tôi, giải quyết vấn đề này:

chúng tôi cho biết cột nào sẽ được kéo dài trong prop android: strechColumns = "a, b .." bằng danh sách được phân tách bằng dấu phẩy

 <TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/buttonGroup" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="0,1" > 

     <TableRow> 
      <Button 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/button1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/button1Name" 
       android:layout_gravity="right"/> 

      <Button 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/button2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/button2Name" 
       android:layout_gravity="left"/> 
     </TableRow> 
    </TableLayout> 

Tôi hy vọng điều đó sẽ hữu ích.Xin lỗi vì tiếng anh của tôi:/

8

Đối với trung một nút duy nhất trên một hàng duy nhất:

<TableRow android:id="@+id/rowNameHere" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:gravity="center"> 
    <Button android:id="@+id/btnDoStuff" 
      android:text="Do Stuff" /> 
</TableRow> 

Sau khi xem xét các câu trả lời ở trên, tôi túm lấy một vài điều từ mỗi câu trả lời và điều này đã làm việc cho tôi.

+0

điều này đã giúp, cảm ơn rất nhiều! –

Các vấn đề liên quan