2015-09-21 5 views
2

Я использую Android Studio. Мой проект предполагает получение некоторых данных через BLE и отображение их на экране в линейной компоновке.Как увеличить размер линейной компоновки во время выполнения программно?

Мой основной код активности для добавления Вид:

dataLayout.addView(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 

Функция определяется как:

addView(dataEdit, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1)); 

В моем файле XML, необходимая часть выглядит следующим образом:

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="1" 
    android:id="@+id/dataLayout" 
    android:fillViewport="true"> 
</LinearLayout> 

EDIT : Весь файл xml выглядит так:

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollView"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:baselineAligned="true"> 

    <TextView 
     style="?android:attr/listSeparatorTextViewStyle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView2" 
     android:text="Find Device" 
     android:layout_marginTop="5dip" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/scanStatus" /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Scan" 
     android:id="@+id/scan" 
     android:layout_gravity="center_horizontal" 
     android:enabled="false" /> 

    <TextView 
     style="?android:attr/listSeparatorTextViewStyle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView4" 
     android:layout_marginTop="5dip" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/deviceInfo" /> 

    <TextView 
     style="?android:attr/listSeparatorTextViewStyle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView3" 
     android:text="Connect Device" 
     android:layout_marginTop="5dip" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/connectionStatus" /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Connect" 
     android:id="@+id/connect" 
     android:layout_gravity="center_horizontal" 
     android:enabled="false" /> 

    </LinearLayout> 

    <TextView 
     style="?android:attr/listSeparatorTextViewStyle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView7" 
     android:text="Receive" 
     android:textColor="#0B0101" 
     android:layout_marginTop="5dip" /> 

    <Button 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:text="Clear Data" 
     android:textColor="#FFFCFC" 
     android:id="@+id/clearData" 
     android:layout_gravity="center_horizontal" 
     android:singleLine="false" /> 

    <TextView 
     style="?android:attr/listSeparatorTextViewStyle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView9" 
     android:text="Graphs" 
     android:layout_marginTop="5dip" /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Plot" 
     android:id="@+id/Plot" 
     android:layout_weight="1" 
     android:layout_marginTop="2dip" 
     android:singleLine="false"/> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    </LinearLayout> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Distance:" 
     android:textColor="#0B0101" 
     android:id="@+id/textView8" /> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:stretchColumns="1" 
     android:id="@+id/dataLayout" 
     android:fillViewport="true"> 
    </LinearLayout> 

    </LinearLayout> 

</ScrollView> 

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

Любая помощь будет оценена по достоинству.

+0

Я обновил свой ответ, сообщите нам, если он работает! – bonnyz

+0

@bonnyz Я пробовал код, который вы обновили. Это сделало макет более централизованным и лучше выглядящим, но проблема отображения только первых нескольких значений существует. – user3863537

ответ

1

Просто оберните корневую компоновку ScrollView (how to).

UPDATE

Попробуйте обновить макет следующим образом:

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollView"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:baselineAligned="true"> 

    <TextView 
     style="?android:attr/listSeparatorTextViewStyle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView2" 
     android:text="Find Device" 
     android:layout_marginTop="5dip" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/scanStatus" /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Scan" 
     android:id="@+id/scan" 
     android:layout_gravity="center_horizontal" 
     android:enabled="false" /> 

    <TextView 
     style="?android:attr/listSeparatorTextViewStyle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView4" 
     android:layout_marginTop="5dip" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/deviceInfo" /> 

    <TextView 
     style="?android:attr/listSeparatorTextViewStyle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView3" 
     android:text="Connect Device" 
     android:layout_marginTop="5dip" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/connectionStatus" /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Connect" 
     android:id="@+id/connect" 
     android:layout_gravity="center_horizontal" 
     android:enabled="false" /> 

    </LinearLayout> 

    <TextView 
     style="?android:attr/listSeparatorTextViewStyle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView7" 
     android:text="Receive" 
     android:textColor="#0B0101" 
     android:layout_marginTop="5dip" /> 

    <Button 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:text="Clear Data" 
     android:textColor="#FFFCFC" 
     android:id="@+id/clearData" 
     android:layout_gravity="center_horizontal" 
     android:singleLine="false" /> 

    <TextView 
     style="?android:attr/listSeparatorTextViewStyle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView9" 
     android:text="Graphs" 
     android:layout_marginTop="5dip" /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Plot" 
     android:id="@+id/Plot" 
     android:layout_weight="1" 
     android:layout_marginTop="2dip" 
     android:singleLine="false"/> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Distance:" 
     android:textColor="#0B0101" 
     android:id="@+id/textView8" /> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="1" 
     android:id="@+id/dataLayout"> 
    </LinearLayout> 

    </LinearLayout> 
</ScrollView> 
+0

Я добавил свиток, но он не имеет никакого эффекта. Я редактировал свой вопрос, чтобы добавить полный .xml-файл. Не могли бы вы посмотреть? – user3863537

1

Обертывание ваш LinearLayout в ScrollView должен решить. Однако, вместо того, чтобы добавлять виды самостоятельно, используйте ListView или RecyclerView. Они перезапускают представления, которые не видны для сохранения памяти.

+0

Я завернул весь свой код в виде прокрутки, но он не имеет никакого эффекта. Пожалуйста, взгляните на отредактированную версию моего вопроса, которая содержит весь .xml-файл – user3863537

+0

@ user3863537 Вы считали «ListView» или «RecyclerView»? –

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