2015-09-30 6 views
1

Я начинающий программист для Android с большим количеством фона Java, в основном на мэйнфрейме. (Да, он работает на мэйнфрейме). Я пишу приложение, которое отслеживает ежедневную активность, а после RealativeLayout по умолчанию я добавляю TableLayout через XML. В моем onWindowFocusChanged я также пытаюсь установить ширину TableLayout. Когда я измеряю два, RelativeLayout равно 1024, размер экрана, но TableLayout равен 960. Ниже приведены фрагменты XML и кода. Любая помощь будет очень признательна, поскольку я сделал много поисков и много Doc, и до сих пор не могу добиться этого.Ширина TableLayout не соответствует RelativeLayout width

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/WeekTbar" 
    android:stretchColumns="*" 
    android:shrinkColumns="*" 
    android:layout_alignParentLeft="true" 
    android:id="@+id/table" 
    android:background="#d4fbff"> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#0ff2D2Bf" 
     > 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText" 
      android:layout_column="0" 
      android:background="#2d2bff" 
      android:textColor="#ffffff" 
      android:text="  " /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText2" 
      android:layout_column="1" 
      android:text="Sun" 
      android:textColor="#ffffff" 
      android:background="#2d2bff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText3" 
      android:layout_column="2" 
      android:text="Mon" 
      android:background="#2d2bff" 
      android:textColor="#ffffff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText4" 
      android:layout_column="3" 
      android:text="Tue" 
      android:textColor="#ffffff" 
      android:background="#2d2bff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText6" 
      android:layout_column="4" 
      android:text="Wed" 
      android:background="#2d2bff" 
      android:textColor="#ffffff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText5" 
      android:layout_column="5" 
      android:text="Thu" 
      android:textColor="#ffffff" 
      android:background="#2d2bff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText8" 
      android:layout_column="6" 
      android:text="Fri" 
      android:background="#2d2bff" 
      android:textColor="#ffffff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText9" 
      android:layout_column="7" 
      android:text="Sat" 
      android:textColor="#ffffff" 
      android:background="#2d2bff" /> 

    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#0ff2D2Bf" 
     android:focusableInTouchMode="true"> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText10" 
      android:layout_column="0" 
      android:text="Steps" 
      android:background="#2d2bff" 
      android:textColor="#ffffff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/SunSteps" 
      android:layout_column="1" 
      android:background="#2d2bff" 
      android:textColor="#ffffff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/MonSteps" 
      android:layout_column="2" 
      android:textColor="#ffffff" 
      android:background="#2d2bff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/TueSteps" 
      android:layout_column="3" 
      android:background="#2d2bff" 
      android:textColor="#ffffff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/WedSteps" 
      android:layout_column="4" 
      android:textColor="#ffffff" 
      android:background="#2d2bff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/ThuSteps" 
      android:layout_column="5" 
      android:background="#2d2bff" 
      android:textColor="#ffffff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/FriSteps" 
      android:layout_column="6" 
      android:textColor="#ffffff" 
      android:background="#2d2bff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/SatSteps" 
      android:layout_column="7" 
      android:background="#2d2bff" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#0ff2D2Bf"> 


     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText11" 
      android:layout_column="0" 
      android:text=Cals" 
      android:textColor="#ffffff" 
      android:background="#2d2bff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/SunCals" 
      android:layout_column="1" 
      android:focusableInTouchMode="true" 
      android:background="#2d2bff" 
      android:textColor="#ffffff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/MonCals" 
      android:layout_column="2" 
      android:textColor="#ffffff" 
      android:focusableInTouchMode="true" 
      android:background="#2d2bff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/TueCals" 
      android:layout_column="3" 
      android:background="#2d2bff" 
      android:focusableInTouchMode="true" 
      android:textColor="#ffffff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/WedCals" 
      android:layout_column="4" 
      android:textColor="#ffffff" 
      android:focusableInTouchMode="true" 
      android:background="#2d2bff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/ThuCals" 
      android:layout_column="5" 
      android:background="#2d2bff" 
      android:focusableInTouchMode="true" 
      android:textColor="#ffffff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/FriCals" 
      android:layout_column="6" 
      android:textColor="#ffffff" 
      android:focusableInTouchMode="true" 
      android:background="#2d2bff" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText7" 
      android:layout_column="7" 
      android:background="#2d2bff" 
      android:focusableInTouchMode="true" 
      android:textColor="#ffffff" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#0ff2D2Bf"> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText12" 
      android:layout_column="0" 
      android:text="Wgt" 
      android:background="#2d2bff" 
      android:textColor="#ffffff" /> 
    </TableRow> 

</TableLayout> 

Java код

public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    if (hasFocus == true) { 
     TableLayout table = (TableLayout) findViewById(R.id.table); 
     RelativeLayout rl = (RelativeLayout)findViewById(R.id.RelativeLayout); 
     int layoutWidth = rl.getWidth(); 
     int layoutHeight = rl.getHeight(); 
     table.getLayoutParams().width = layoutWidth; 
     table.setMinimumWidth(layoutWidth); 
     gw = table.getWidth(); 
     rw = layoutWidth; 
     int x =1; 
    } 
} 
+0

Пожалуйста, покажите ваш XML-код RelativeLayout –

+0

Добавление RelativeLayout XML. – user3112733

+0

ответ

0

Если вы хотите только ваш макет таблицы ширина спичку RelativeLayout ширине вам не нужно поддерживать несколько элементов XML. Ваша проблема может возникнуть, если вы поддерживаете android:padding в RelativeLayout или android:layout_margin в TableLayout. Проверьте простой код ниже

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:padding="10dp" // remove it 
     android:layout_height="match_parent" > 
     ... 
     <TableLayout 
      android:id="@+id/tableLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#ff0000" 
      android:layout_margin="10dp" // remove it 
     > 

      <TableRow 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       .... > 

      </TableRow> 
      .... 
     </TableLayout> 
    </RelativeLayout> 

Подробнее о андроида края макета и дополнения в this Надеюсь, что это помощь

Смежные вопросы