【问题标题】:Android LinearLayout not displayingAndroid LinearLayout 不显示
【发布时间】:2014-01-11 18:49:58
【问题描述】:

我的 xml 有问题。我有一个包含图表(aChartEngine 库)的 LinearLayout,如果我将高度设置为 480dp,它会显示图表,如果我将高度设置为 wrap_content,则不会显示任何内容。我觉得是家长有问题什么的。这是整个 xml 文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".Sniffer" >

<TabHost
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@color/black" >

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

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:orientation="vertical" >

                   <!-- removed some content here, 2 much space and not relevant -->

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:orientation="vertical" >

                    <View
                      android:layout_width="1dp"
                      android:layout_height="30dp">
                    </View>

                    <View
                        style="@style/Divider"
                        android:background="@color/semi_transparent"/>

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:orientation="vertical" >

                    <View
                      android:layout_width="1dp"
                      android:layout_height="30dp">
                    </View>

                    <View
                        style="@style/Divider"
                        android:background="@color/semi_transparent"/>

                </LinearLayout>

                 <LinearLayout
                    android:id="@+id/tab4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:orientation="vertical" >

                    <View
                      android:layout_width="1dp"
                      android:layout_height="30dp">
                    </View>

                    <LinearLayout
                        android:id="@+id/chart"
                        android:layout_width="match_parent"
                        android:layout_height="500dp"                    
                        android:orientation="horizontal">
                    </LinearLayout>

                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </ScrollView>
</TabHost>

在 tab4 下有一个带有图表 ID 的 LinearLayout。那是我放置图表的地方。我尝试使用 weight 属性和 0dip 但仍然无法正常工作,只有固定高度。

LE:LinearLayout 是空的,因为我以编程方式向其中添加内容。

图表的代码 sn-p。

public void createGraphCPU(List<BasicNameValuePair> values){

    mSeries = new CategorySeries("");
    mRenderer = new DefaultRenderer();
    statisticsTab.removeAllViews();

    Collections.shuffle(COLORS_LIST);
    for(int i=0;i<values.size();i++){
        double value=Double.parseDouble(values.get(i).getValue());
        mSeries.add( values.get(i).getName(),value );
        SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
        renderer.setColor(COLORS_LIST.get(i%COLORS_LIST.size()));
        renderer.setDisplayChartValues(true);
        mRenderer.addSeriesRenderer(renderer);
    }

     mRenderer.setChartTitleTextSize(20); 
     mRenderer.setChartTitle("CPU Energy Consumption");
     mRenderer.setLabelsTextSize(15);  
     mRenderer.setLegendTextSize(15);  
     mRenderer.setMargins(new int[] { 20, 15, 15, 0 });  
     mRenderer.setZoomButtonsVisible(true);  

     if (mChartView != null) {
          mChartView.repaint();
    }
     else{
         mChartView = ChartFactory.getPieChartView(this, mSeries,mRenderer);
        statisticsTab.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
     }

}

【问题讨论】:

  • id为chart的LinearLayout,看起来是空的,因为里面没有任何项目?
  • 是的,很抱歉没有指定,我以编程方式在其中添加项目。它只是来自 aChartEngine 库的 GraphicView,但它就在那里
  • 那么,问题可能出在您分配给图表视图的布局参数上。你能分享你添加图表视图的代码吗?
  • 你在使用 LinearLayout.LayoutParams 吗?

标签: android android-layout


【解决方案1】:

您说当您为android:layout_height 指定固定高度时可以看到您的图表,而当您使用wrap_content 时您看不到您的图表。如果是这样,这种行为的原因是您的图表本身没有提供高度——实际上,您将fill_parent 指定为其宽度和高度。因此,如果您的 LinearLayout 想要与您的图表 (wrap_content) 一样小,而您的图表想要与其周围的 LinarLayout (fill_parent) 一样大,Android 将根本不会显示您的图表。

然后解决方案可能是更好地问问自己,您想为图表提供什么宽度和高度。

【讨论】:

  • 目前我有三个图表,每个图表都在不同的线性布局上。 Atm 每个线性布局都有一个高度,它后面的图表被前一个线性布局剪裁。让我尝试为图表本身设置一个固定高度,看看剪裁是否消失。
  • 找不到指定图表本身高度的方法,我必须使用包含它的 LinearLayout 的高度,我认为没有其他选择。如果有人使用过图表引擎并且知道如何使用,请提供一些见解。话虽如此,我如何制作 3 个高度为 400dp 的线性布局让我们说一个低于另一个而不是剪辑。例如,我的第二个图表被剪辑到一半。我尝试将它们包装成一个RelativeLayout并使用layout_below,但图表仍然被裁剪。
  • 如果您想在可滚动视图中查看您的视图,您需要使用ListViewLinearLayout 不是解决您问题的正确方法。当您使用 ListView 时,为每个列表项应用 400dp 的视图高度。
  • 您的问题是您在 ScrollView 中使用 LinearLayout。这是错误的。仅使用 ListView。没有线性布局。我不能给你更多的帮助。
猜你喜欢
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 2021-11-19
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-27
相关资源
最近更新 更多