Android - 在ListView上方添加Linearlayout

时间:2014-03-12 23:57:25

标签: android android-layout android-listview android-linearlayout android-ui

我想在LinearLayout上方添加ListView,,以便在滚动屏幕时滚动整个布局。在我的LinearLayout中,我有一个AChartEngine GraphicalView,我想在ListView之上。我添加了ScrollView,其中包含SpinnerLinearLayout ListView,但是当我运行应用时,只有ListView和{{1} } 出现。 Spinner未显示在屏幕上。

这是我的xml文件。感谢。

LinearLayout

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

当我调用返回图形的方法时,我会获得一个View,我需要将其添加到Layout中。我知道这样做的唯一方法是将它添加到LinearLayout。

<Spinner
    android:id="@+id/spending_report_cycle_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

  <LinearLayout
    android:id="@+id/spending_report_graph"
    android:layout_width="wrap_content"
    android:layou`enter code here`t_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center_horizontal" />

<ListView
    android:id="@+id/spending_report_listview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</ListView>

1 个答案:

答案 0 :(得分:2)

这是你要实现的吗?

Test Image http://imageshack.com/a/img43/3327/f091.png

如果是,那么描述就像是,Item1是微调器,底部的Item 2是ListView,中间的黑线是View,extire布局是线性布局,它有一个子节点(Scroll View)并且只有一个滚动视图的直接子项是线性布局。

这是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:orientation="vertical" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

            <Spinner
                android:id="@+id/spending_report_cycle_spinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <View
                android:id="@+id/spending_report_graph"
                android:layout_width="wrap_content"
                android:layout_height="3dp"
                android:layout_gravity="center_horizontal"
                android:background="#000000"
                android:orientation="vertical" />

            <ListView
                android:id="@+id/spending_report_listview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </ListView>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

以下是经过编辑的答案:

Test Image http://imageshack.com/a/img809/7741/ti70.png

这是我使用片段只是为了确认一切正常,是的,一切都很完美。

如你所见,我正在使用的片段,微调器位于顶部,列表视图位于底部,执行代码后,视图如下:

Test Image 2 http://imageshack.com/a/img713/685/qpbi.png

这里,两条绿线内的视图是片段。这是一切的代码:

首先:yourmainlayout.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:orientation="vertical" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

            <Spinner
                android:id="@+id/spending_report_cycle_spinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <View
                android:id="@+id/SomeView2"
                android:layout_width="wrap_content"
                android:layout_height="5dp"
                android:layout_gravity="center_horizontal"
                android:background="#008080"
                android:orientation="vertical" />

            <fragment
                android:id="@+id/fragment_content_1"
                android:name="com.mike.passintents.Fragment1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <View
                android:id="@+id/SomeView2"
                android:layout_width="wrap_content"
                android:layout_height="5dp"
                android:layout_gravity="center_horizontal"
                android:background="#008080"
                android:orientation="vertical" />

            <ListView
                android:id="@+id/spending_report_listview"
                android:layout_width="fill_parent"
                android:layout_height="300dp"
                android:background="#333333" >
            </ListView>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

第二名:Fragment1

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mike.stackoverflowquestions.R;

public class Fragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_fragment_1, container, false);

    }

}

第三:主要活动

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.mike.stackoverflowquestions.R;

public class ActivityA extends Activity {

    String somevalue1 = "Hello";
    String somevalue2 = "World";
    ListView mListView;
    String[] numbers_text = new String[] { "one", "two", "three", "four",
            "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
            "thirteen", "fourteen", "fifteen" };
    ArrayList<String> mArrayList;
    ArrayAdapter<String> mAdapter;
    ArrayAdapter<String> spinnerAdapter;
    Spinner spinner1;
    TextView tV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_view_stack);
        mArrayList = new ArrayList<String>();

        for (String s : numbers_text) {

            mArrayList.add(s);

        }
        spinner1 = (Spinner) findViewById(R.id.spending_report_cycle_spinner);
        tV = (TextView) findViewById(R.id.tv);
        mListView = (ListView) findViewById(R.id.spending_report_listview);
        mAdapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_1, mArrayList);
        mListView.setAdapter(mAdapter);
        spinnerAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item,
                mArrayList);
        spinner1.setAdapter(spinnerAdapter);

    }

    public void selectFragment(View view) {

        Fragment fr;

        if (view == findViewById(R.id.btnSayHi)) {

            fr = new Fragment1();

        } else {

            fr = new Fragment1();

        }

        FragmentManager fm = getFragmentManager();
        FragmentTransaction mFragmentTransaction = fm.beginTransaction();
        mFragmentTransaction.replace(R.id.fragment_content_1, fr);
        mFragmentTransaction.commit();

    }

}

我做了编辑。如果有效,请告诉我。祝你好运.. :)