【问题标题】:Android - Add Linearlayout above ListViewAndroid - 在 ListView 上方添加 Linearlayout
【发布时间】:2014-03-14 05:08:59
【问题描述】:

我想在ListView, 上方添加一个LinearLayout,这样当您滚动屏幕时,它会滚动整个布局。在我的LinearLayout 中,我有一个 AChartEngine GraphicalView,我想要它在 ListView 之上。我添加了ScrollView,在其中添加了SpinnerLinearLayoutListView,但是当我运行应用程序时,只有ListViewSpinner 出现。 LinearLayout 没有出现在屏幕上。

这是我的 xml 文件。谢谢。

<?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" >

<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>

当我调用返回我的图表的方法时,我得到了一个视图,我需要将它添加到布局中。我知道的唯一方法是将其添加到 LinearLayout。

    graphLayout = (LinearLayout)view.getViewById(R.id.spending_report_graph);
    graph = PieGraph.getNewInstance(getActivity(), items, items.size());
    graphLayout.addView(graph);

【问题讨论】:

  • 你不能把这些视图添加到 ListView 标题中吗?
  • 我试过了,但是它将 LinearLayout 的高度限制为 ListBlock 的高度。 LinearLayout 的高度大于 ListBlock 的高度。
  • 您确定您的布局正确吗?因为滚动视图只能有一个直接子项。
  • 您是否尝试过为支出报告图提供固定高度?因为可能存在从未创建过的图表的情况,因此 wrap_content 将设置为 0dp。

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


【解决方案1】:

这是你想要实现的吗?

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

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

这是它的 xml。我不确定这是否是您要找的。​​p>

<?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>

第二个:片段1

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();

    }

}

我进行了编辑。请让我知道这是否有效。祝你好运..:)

【讨论】:

  • 其实差不多,但不完全一样。我想在 Spinner 和 ListView 之间放置一个视图,但是以编程方式。当我调用返回我的图表的方法时,我得到一个视图,我需要将它添加到我的布局中。 (我已经用生成图表的代码更新了我的问题)。我想要的实际上与此更相似:imgur.com/g5Er23M
  • 您是否尝试使用片段而不是该视图?如果您使用片段,则可以在片段类中的片段中显示您的图表。
  • 是的,我正在使用片段。我将图表放在片段中,在 ListView 上方,但是当我添加图表时,listview 根本不显示。
  • 谢谢!它就像我想要的那样工作。非常感谢您的帮助。
  • 很高兴为您提供帮助.. :)
猜你喜欢
  • 1970-01-01
  • 2020-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 1970-01-01
  • 2011-12-30
  • 1970-01-01
相关资源
最近更新 更多