2016-07-31 1 views
0

Я довольно новичок в оформлении android и xml. Я пытался сделать приложение и уже сделал с помощью бит программирования. Однако я застрял в финализации макета. На самом деле нужна рука. Мое приложение должно выглядеть следующим образом:Сделать линейную компоновку не выходить из экрана

Layout

Я разделил экран на 4 линейных макетах, как я пронумеровал в картине, как показано ниже:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context="com.FirstVersion.android.cwd.Play" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:background="@color/material_blue_grey_800"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/btnGoBack" 
     android:text="Go Back" 
     android:layout_weight="1" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Profile Information" 
     android:background="@color/accent_material_dark" 
     android:layout_weight="10" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/LDrawerAction" 
     android:text="Menu" 
     android:clickable="false" 
     android:layout_weight="1" 
     android:layout_marginRight="5dp" 
     android:layout_marginLeft="5dp" /> 

</LinearLayout> 

//endregion 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/Section2" 
android:orientation="vertical"> 

</LinearLayout> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/Section3" 
android:orientation="horizontal"> 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="5sp" 
    android:textColor="@color/colorWhite" 
    android:background="#990000" 
    android:maxLines = "3" 
    android:scrollbars = "vertical" 
    android:id="@+id/txtSection3" /> 

</LinearLayout> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:weightSum="15" 
android:orientation="vertical" 
android:baselineAligned="false"> 

    <TableLayout 
    android:id="@+id/Section4" 
    android:layout_width="match_parent" 
    android:layout_weight="15" 
    android:layout_height="wrap_content"> 

    </TableLayout> 
</LinearLayout> 

</LinearLayout> 
</android.support.v4.widget.DrawerLayout> 

Сейчас проблема заключается в том, что, вторая секция заполняется программно и имеет диапазон строк от 8 до 14. То же самое для столбцов. Каждый блок представляет собой разные кнопки, которые я могу добавить этот код:

Lay= (LinearLayout)findViewById(R.id.Section2); 
for (int row = 0; row < numberOfRows; row++) 
    { 
     LinearLayout Linearrow = new LinearLayout(this); 
     Linearrow.setOrientation(LinearLayout.HORIZONTAL); 

     for (int column = 0; column < numberOfColumns; column++) 
     { 
      View a = Chosen.blocks[row][column]; //This is a View the extend Button that has no layout properties defined 

      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT, 1.0f); 

      a.setLayoutParams(params); 

      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
      lp.weight = 1.0f; 
      Linearrow.addView(a, lp); 
     } 
     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
     lp.weight = 1.0f; 
     Lay.addView(Linearrow, lp); 
    } 

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

TL =(TableLayout)fa.findViewById(R.id.Section2); 

    for (int i = 0; i < numberOfRows; i++) 
    { 
     TableRow tableRow = new TableRow(c); 

     for(int j = 0; j < numberOfColumns; j++) 
     { 
      String s = T[(i * 10) + j]; 
      Block b = new Block(c, s); 
      //int w = Math.round(c.getResources().getDisplayMetrics().density * 20); 
      //int h = Math.round(c.getResources().getDisplayMetrics().density * 80); 
      //b.setLayoutParams(new TableRow.LayoutParams(w, h, 2.0f)); 

      b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f)); 
      b.setBackgroundResource(R.drawable.g_3); 
      tableRow.addView(b); 
      tableRow.setLayoutParams(new android.widget.LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 5f)); 

      final Block fb = b; 
     } 
     TL.addView(tableRow); 
    } 

Когда я запустил приложение, я вижу, что, если я его не открываю на большом экране, содержимое не будет видно полностью (как если бы высота была установлена ​​вручную вручную из макетов), и поскольку она не имеет возможности прокрутки, содержимое не видно. Как я могу изменить или изменить макет, чтобы он выглядел как на картинке выше?

+0

Для столбцов макета таблицы, вы можете найти натяжные столбцы атрибутов полезно –

+0

Почему бы вам не использовать 'android.support.v7.widget.RecyclerView' для этого динамического контента? В другом случае добавьте макет, который прокручивает с именем 'android.support.v4.widget.NestedScrollView' как родительский элемент в LinearLaout с высотой и шириной как' match_parent'. – JavaGhost

+0

@ cricket_007 Я уже установил его как свойство и не имеет никакого значения. – blu3f1rest0rm

ответ

0

Make фиксированной высоты макетов с использованием layout_weight

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.1" 
     android:weightSum="12" 
     android:orientation="horizontal" 
     android:background="@color/material_blue_grey_800"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnGoBack" 
      android:text="Go Back" 
      android:layout_weight="1" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Profile Information" 
      android:background="@color/accent_material_dark" 
      android:layout_weight="10" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/LDrawerAction" 
      android:text="Menu" 
      android:clickable="false" 
      android:layout_weight="1" 
      android:layout_marginRight="5dp" 
      android:layout_marginLeft="5dp" /> 

    </LinearLayout> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.4" 
     android:weightSum="1" 
     android:id="@+id/Section2" 
     android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.1" 
     android:id="@+id/Section3" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textSize="5sp" 
      android:textColor="@android:color/white" 
      android:background="#990000" 
      android:maxLines = "3" 
      android:scrollbars = "vertical" 
      android:id="@+id/txtSection3" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.4" 
     android:weightSum="15" 
     android:orientation="vertical" 
     android:baselineAligned="false"> 

     <TableLayout 
      android:id="@+id/Section4" 
      android:layout_width="match_parent" 
      android:layout_weight="15" 
      android:layout_height="wrap_content"> 

     </TableLayout> 
    </LinearLayout> 

</LinearLayout> 
</android.support.v4.widget.DrawerLayout> 

И в вашем классе Java, где вы добавляете взгляды динамически, вычислить вес детей соответственно, как указано ниже

Lay= (LinearLayout)findViewById(R.id.Section2); 
Lay.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); 
int totalWidth = Lay.getMeasuredWidth(); 
float rowWeight = 1/numberOfRows; 
int columnWidth = totalWidth/numberOfColumns; 
for (int row = 0; row < numberOfRows; row++) 
{ 
    LinearLayout Linearrow = new LinearLayout(this); 
    Linearrow.setOrientation(LinearLayout.HORIZONTAL); 

    for (int column = 0; column < numberOfColumns; column++) 
    { 
     View a = Chosen.blocks[row][column]; //This is a View the extend Button that has no layout properties defined 
     // height is determined by row's layout weight 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(columnWidth, 
       LinearLayout.LayoutParams.MATCH_PARENT, 1.0f); 

     a.setLayoutParams(params); 
     Linearrow.addView(a); 
    } 
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
    lp.weight = rowWeight; 
    Lay.addView(Linearrow, lp); 
} 

Я не тестировал их, но мог бы работать с небольшими изменениями, которые вы можете выяснить после запуска этого кода.

+0

Я попробовал решение здесь и добавил даже указанный вами метод в комментариях к вопросу. То, что я делал неправильно, было высотой секций, так как с помощью веса, необходимого всем, чтобы получить высоту 0d и не только некоторые. Спасибо – blu3f1rest0rm

0

Использование

android:layout_weight 

атрибут для организации в равных частях в линейной компоновке. Престола в Link

+1

Я уже установил вес элементов внутри линейного. Проблема заключается в самом линейномLayout, так как он распространяется на экран при добавлении элементов. Я даже добавляю сумму веса. – blu3f1rest0rm

+0

Добавьте вес к линейным макетам, чтобы они фиксировали высоту на основе размера экрана. Затем, если вы добавите детей, они будут вписываться. – JavaGhost

+0

@JavaGhost Будет ли он соответствовать экрану при динамической загрузке представлений? –

0

Лучшее использование Просмотр внутри ScrollView. Используйте следующий код: -

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.FirstVersion.android.cwd.Play" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:background="@color/material_blue_grey_800"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/btnGoBack" 
      android:text="Go Back" 
      android:layout_weight="1" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Profile Information" 
      android:background="@color/accent_material_dark" 
      android:layout_weight="10" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/LDrawerAction" 
      android:text="Menu" 
      android:clickable="false" 
      android:layout_weight="1" 
      android:layout_marginRight="5dp" 
      android:layout_marginLeft="5dp" /> 

    </LinearLayout> 

    //endregion 
    <ScrollView 
android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/Section2" 
    android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/Section3" 
    android:orientation="horizontal"> 

     <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="5sp" 
     android:textColor="@color/colorWhite" 
     android:background="#990000" 
     android:maxLines = "3" 
     android:scrollbars = "vertical" 
     android:id="@+id/txtSection3" /> 

    </LinearLayout> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="15" 
    android:orientation="vertical" 
    android:baselineAligned="false"> 

     <TableLayout 
     android:id="@+id/Section4" 
     android:layout_width="match_parent" 
     android:layout_weight="15" 
     android:layout_height="wrap_content"> 

     </TableLayout> 
    </LinearLayout> 
    </LinearLayout> 
</ScrollView> 
    </LinearLayout> 
    </android.support.v4.widget.DrawerLayout> 
+0

Как я указал в вопросе, я не использую scrollview, и я не могу, так как мне нужно показать всю информацию – blu3f1rest0rm

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

  • Нет связанных вопросов^_^