2012-11-22 4 views
1

동일한 크기의 버튼으로 된 XML 그리드 테이블 레이아웃 메뉴가 있습니다. 버튼은 전화 해상도에 따라 크기가 바뀝니다. 아이디어는 메뉴가 동일한 크기의 버튼으로 화면을 채우는 것입니다. 버튼에 텍스트 레이블이 없으면 완벽하게 작동합니다. 그러나 단추에 텍스트를 추가하자마자 단추의 너비도 텍스트의 영향을받습니다. 텍스트 크기를 더 작은 크기로 변경하면 버튼이 자동으로 다른 크기보다 작아집니다. 예를 들어, 버튼 A의 이름은 "집"입니다. 버튼 B의 이름은 "알림"입니다. 문제는 버튼 B가 버튼 A보다 커진다는 것입니다. 모든 버튼이 레이블 텍스트 크기와 동일한 너비를 가지지 않기를 바랍니다. XML 레이아웃도 첨부 할 예정입니다. 그것을 할 수있는 가장 좋은 방법은 있지만, 경우Android : 버튼 크기를 변경하지 않고 글꼴 크기를 변경하는 방법

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="4dp" 
    android:background="#2b2c31" 
    android:orientation="vertical" > 

    <include 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     layout="@layout/layout_topheader" /> 


     <TableLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="10dp" 
      android:stretchColumns="*" > 

       <TableRow 
      android:layout_weight="1" 
      android:gravity="center" 
      android:padding="10dp" > 

        <Button 
         android:id="@+id/buttonScan" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:layout_weight="1" 
         android:background="@drawable/btn" 
         android:text="Refill"        
         android:layout_margin="10dp" 
         android:textColor="@color/status_text" 
         android:textSize="30dip" 
         android:textStyle="bold" /> 

        <Button 
         android:id="@+id/buttonSetReminder" 
         android:layout_width="fill_parent"        
         android:layout_height="fill_parent" 
         android:layout_weight="1" 
         android:background="@drawable/btn" 
         android:text="Reminders"       
         android:layout_margin="10dp" 
         android:textColor="@color/status_text" 
         android:textSize="30dip" 
         android:textStyle="bold" /> 

       </TableRow> 

       <TableRow 
      android:layout_weight="1" 
      android:gravity="center" 
      android:padding="10dp" > 

        <Button 
         android:id="@+id/buttonRewards" 
         android:layout_width="fill_parent"        
         android:layout_height="fill_parent" 
         android:layout_weight="1" 
         android:background="@drawable/btn" 

         android:layout_margin="10dp" 
         android:textSize="30dip" 
         android:textColorHint="@color/status_text" 
         android:textColor="@color/status_text" 
         android:text="Rewards" 
         android:textStyle="bold" /> 

        <Button 
         android:id="@+id/buttonTracker" 
         android:layout_width="fill_parent"        
         android:layout_height="fill_parent" 
         android:layout_weight="1" 
         android:background="@drawable/btn" 
         android:text=" Tracker" 

         android:layout_margin="10dp" 
         android:textColor="@color/status_text" 
         android:textSize="30dip" 
         android:textStyle="bold" /> 

       </TableRow> 

       <TableRow 
        android:layout_weight="1" 
        android:gravity="center" 
        android:padding="10dp" > 

        <Button 
         android:id="@+id/buttonProfile" 
         android:layout_width="fill_parent"        
         android:layout_height="fill_parent" 
         android:layout_weight="1" 
         android:background="@drawable/btn" 
         android:text="Profile"       
         android:layout_margin="10dp" 
         android:textColor="@color/status_text" 
         android:textSize="30dip" 
         android:textStyle="bold" /> 

        <Button 
         android:id="@+id/buttonChat" 
         android:layout_width="fill_parent"        
         android:layout_height="fill_parent" 
         android:layout_weight="1" 
         android:background="@drawable/chaticon" 
         android:text="Chat"       
         android:layout_margin="10dp" 
         android:textColor="@color/status_text" 
         android:textSize="30dip" 
         android:textStyle="bold" /> 

       </TableRow> 

        <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="#ffffff" 
      android:orientation="vertical" > 


       <ImageView 
        android:id="@+id/imageView1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:src="@drawable/add3" /> 

     </LinearLayout>      

    </TableLayout>   

</LinearLayout> 

답변

0

확실하지, 나는 비슷한 문제를 가지고와 함께 실행시 화면 크기를 얻어서 그것을 해결 : 그런 다음 버튼 폭을 설정

DisplayMetrics displaymetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 

as : int buttonWidth = displaymetrics.widthPixels/2;

마지막으로 maxWidth와 MinWidth/height를 buttonWidth로 설정하십시오.

관련 문제