2016-02-27 4 views
2

Я пытаюсь создать приложение в студии android, и по некоторым причинам один gridLayout решает не появляться, несмотря на то, что он занимает необходимое пространство на экране. Image from android studio, actual screenshot from my phoneGridlayout не отображается, Android studio

XML файл макета:

<?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:background="#cbcde6" 
 
    android:orientation="vertical" 
 
    android:gravity="top|center"> 
 
    <TextView 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:text="Insert data" 
 
     android:textAppearance="?android:attr/textAppearanceLarge" 
 
     /> 
 

 
    <GridLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:orientation="horizontal" 
 
     android:layout_gravity="top" 
 
     android:columnCount="3" 
 
     android:rowCount="3" 
 
     android:visibility="visible"> 
 
     <EditText 
 
      android:background="#ffffff" 
 
      android:layout_height="30sp" 
 
      android:layout_margin="15dp" 
 
      android:layout_columnWeight="1"/> 
 
     <EditText 
 
      android:background="#ffffff" 
 
      android:layout_height="30sp" 
 
      android:layout_margin="15dp" 
 
      android:layout_columnWeight="1"/> 
 
     <EditText 
 
      android:background="#ffffff" 
 
      android:layout_height="30sp" 
 
      android:layout_margin="15dp" 
 
      android:layout_columnWeight="1"/> 
 
     <EditText 
 
      android:background="#ffffff" 
 
      android:layout_height="30sp" 
 
      android:layout_margin="15dp" 
 
      android:layout_columnWeight="1"/> 
 
     <EditText 
 
      android:background="#ffffff" 
 
      android:layout_height="30sp" 
 
      android:layout_margin="15dp" 
 
      android:layout_columnWeight="1"/> 
 
     <EditText 
 
      android:background="#ffffff" 
 
      android:layout_height="30sp" 
 
      android:layout_margin="15dp" 
 
      android:layout_columnWeight="1"/> 
 
     <EditText 
 
      android:background="#ffffff" 
 
      android:layout_height="30sp" 
 
      android:layout_margin="15dp" 
 
      android:layout_columnWeight="1"/> 
 
     <EditText 
 
      android:background="#ffffff" 
 
      android:layout_height="30sp" 
 
      android:layout_margin="15dp" 
 
      android:layout_columnWeight="1"/> 
 
     <EditText 
 
      android:background="#ffffff" 
 
      android:layout_height="30sp" 
 
      android:layout_margin="15dp" 
 
      android:layout_columnWeight="1"/> 
 

 
    </GridLayout> 
 

 
    <LinearLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:orientation="vertical" 
 
     android:gravity="center"> 
 
     <Button 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:text="Calculate" 
 
      android:textAppearance="?android:attr/textAppearanceLarge"/> 
 
     <LinearLayout 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:orientation="horizontal" 
 
      android:gravity="center" 
 
      > 
 
      <TextView 
 
       android:layout_width="wrap_content" 
 
       android:layout_height="wrap_content" 
 
       android:text="Result is: " 
 
       android:textAppearance="?android:attr/textAppearanceLarge"/> 
 
      <TextView 
 
       android:layout_width="wrap_content" 
 
       android:layout_height="wrap_content" 
 
       android:text="Result" 
 
       android:textAppearance="?android:attr/textAppearanceLarge"/> 
 

 
     </LinearLayout> 
 

 
    </LinearLayout> 
 

 
    <LinearLayout 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="match_parent" 
 
     android:gravity="bottom"> 
 
     <Button 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:text="@string/backButton_text" 
 
      android:id="@+id/back" 
 
      android:onClick="onClickBackButton" 
 
      android:textAppearance="?android:attr/textAppearanceLarge"/> 
 

 
    </LinearLayout> 
 

 
</LinearLayout>

ответ

0

андроида: layout_columnWeight работает только на API 21 или более поздней версии, так как на скриншоте в коммуникаторе им угадывание ниже.

В связи с тем, что edittext не имеет указанной ширины, а также нет текста, определяемого его показанием с шириной 0, вот почему вы, похоже, не видите его.

Я рекомендую определения ширины и полей с помощью кода, чтобы добиться того, что вы показываете на скриншоте студии:

Определить имя для GridLayout, им с помощью в примере «GridLayout»

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    GridLayout g = (GridLayout)findViewById(R.id.gridlayout); 
    int screenWidth = getScreenWidth(); 
    int cellWidth = screenWidth/4; 
    int margin = cellWidth/8; 
    GridLayout.LayoutParams p; 
    for(int i = 0; i < g.getChildCount(); i++){ 
     p = (GridLayout.LayoutParams)g.getChildAt(i).getLayoutParams(); 
     p.width = cellWidth; 
     p.setMargins(margin, margin, margin, margin); 
    } 
} 

private int getScreenWidth(){ 
    Display display = getWindowManager().getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    return size.x; 
} 

It будет одинаковым образом искать любое устройство и любую ориентацию.

+0

Спасибо, зная, что андроид: layout_columnWeight работает только на api 21 или позже (my is api 19), я искал альтернативу, и я нашел это [link] (http://developer.android.com/reference/android/support/percent/packag е-summary.html). –

0

Предоставлять 1-й строки и 1-й столбец

<EditText 
      android:background="#ffffff" 
      android:layout_height="30sp" 
      android:layout_margin="15dp" 
      android:layout_row="0" 
      android:layout_column="0" /> 

первой строки и второго столбца

android:layout_row="0" 
android:layout_column="1" 

сделать это для каждого ребенка зависит от строки и столбца

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