XML文件wrap_content问题

时间:2014-10-01 05:08:25

标签: java android xml android-layout

我有这个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" >

    <RelativeLayout
        android:id="@+id/rlForScreenShot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:layout_centerHorizontal="true" >

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

            ***<com.kite.share.chart.ChartView
                android:id="@+id/cvChart"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                />***

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

                <TextView
                    android:id="@+id/tvCompanyName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Loading" />

                <TextView
                    android:id="@+id/tvCompanyPrice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:text="Loading" />

                <TextView
                    android:id="@+id/tvCompanyChange"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:text="Loading" />
            </LinearLayout>
            </LinearLayout> 
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rlCompanyInfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">"


        <ImageView
            android:id="@+id/ivCompanyLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/tvCompanyAbout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/ivCompanyLogo"
            android:text="@string/txtEmpty" />

        <Button
            android:id="@+id/bShare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/tvCompanyAbout"
            android:text="Share" />

        <Button
            android:id="@+id/btnFollowToggle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/bShare"
            android:text="@string/txtDummy" />
    </RelativeLayout>

</LinearLayout>

但是当我加载调用此布局的活动时,应该显示的图表根本不会显示。但是当我将图表的属性设置为android:layout_height = "100dp"时,它变得可见。 我认为"wrap_content"应该可以工作,而不是在我的xml文件中对其进行硬编码。

请某人提及有关LayoutInflaters的尝试,但也没有为我工作。无论如何我能解决这个问题吗?

这是我的Activity.java文件,用于加载XML文件。

@SuppressLint("ClickableViewAccessibility")
public class CompanyActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {
    ChartView cvChart;
    TextView tvCompanyName;
    TextView tvCompanyPrice;
    TextView tvCompanyChange;
    ImageView ivCompanyLogo;
    TextView tvCompanyAbout;
    RelativeLayout rlForScreenShot;
    Button bShare;
    PullAndLoadListView lvCompanyWall;
    WallAdapter adapter;
    SimpleFacebook mSimpleFacebook;
    Company company;
    Bitmap screenShot;

    int start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.company);

        getFragmentManager().beginTransaction().add(R.id.rlHeader, header).commit();

        Bundle b = this.getIntent().getExtras();
        company = (Company)b.getSerializable("company");

        super.setTitle(company.companyCode);

        flProgress = (FrameLayout)findViewById(R.id.flProgress);

        start = 0;

        View header = getLayoutInflater().inflate(R.layout.company_header, null, false);    
    rlForScreenShot = (RelativeLayout)findViewById(R.id.rlForScreenShot);
        cvChart = (ChartView) header.findViewById(R.id.cvChart);


        if(cvChart != null){
        ChartOptions options;
            String[] columnNames = new String[] { company.companyName };
            cvChart.addGenericChart(columnNames, GenericChart.CHART_TYPE_SPARK_LINE, "chart_div", GenericChart.TITLE_TYPE_EMPTY);
            cvChart.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return (event.getAction() == MotionEvent.ACTION_MOVE);
                }
            });
            options = cvChart.getOptions("chart_div");
            options.setTitle("Sparkline");
            options.addColor("#ffffff");
            options.setAreaOpacity(0.0f);
            options.setBackgroundColor("#000000");
            options.setEnableInteractivity(false);  // Enable in future
        }

        tvCompanyName = (TextView) header.findViewById(R.id.tvCompanyName);
        if (tvCompanyName != null) {
            tvCompanyName.setTypeface(Common.getFontBold(context));
            tvCompanyName.setTextColor(context.getResources().getColor(R.color.black));
        }
        tvCompanyPrice = (TextView) header.findViewById(R.id.tvCompanyPrice);
        if (tvCompanyPrice != null) {
            tvCompanyPrice.setTypeface(Common.getFontBold(context));
            tvCompanyPrice.setTextColor(context.getResources().getColor(R.color.black));
        }
        tvCompanyChange = (TextView) header.findViewById(R.id.tvCompanyChange);
        if (tvCompanyChange != null) {
            tvCompanyChange.setTypeface(Common.getFontBold(context));
            tvCompanyChange.setTextColor(context.getResources().getColor(R.color.black));
        }
        ivCompanyLogo = (ImageView) header.findViewById(R.id.ivCompanyLogo);
        if (ivCompanyLogo != null) {
            UrlImageViewHelper.setUrlDrawable(ivCompanyLogo, 
                    context.getResources().getString(R.string.url_avatar) + "company/" + company.id + company.avatar, 
                    R.drawable.ic_launcher);
        }
        tvCompanyAbout = (TextView) header.findViewById(R.id.tvCompanyAbout);
        if (tvCompanyAbout != null) {
            tvCompanyAbout.setTypeface(Common.getFontRegular(context));
            // Text later
        }

        bShare = (Button) header.findViewById(R.id.bShare);
        bShare.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                rlForScreenShot.setDrawingCacheEnabled(false);
                rlForScreenShot.setDrawingCacheEnabled(true);

                screenShot = rlForScreenShot.getDrawingCache();

                OnPublishListener onPublishListener = new OnPublishListener() {

                    @Override
                    public void onComplete(String response) {
                        // TODO Auto-generated method stub
                        Log.d("OnComplete", "Published successfully. The new post id = " + response);
                    }

                    @Override
                    public void onException(Throwable throwable) {
                        // TODO Auto-generated method stub
                        super.onException(throwable);
                    }

                    @Override
                    public void onFail(String reason) {
                        // TODO Auto-generated method stub
                        super.onFail(reason);
                        Log.d("onFail", "Failed because " + reason);
                    }

                    @Override
                    public void onThinking() {
                        // TODO Auto-generated method stub
                        super.onThinking();
                    }

                };

                String companyAvatarLink = "http://sharefolio.net/avatar/company/" 
                        + company.id
                        + company.avatar;

                String companyLink = "http://sharefolio.net/company.php?id=" + company.id;

                Log.d("AvatarLink", companyAvatarLink);
                Log.d("link", companyLink);

                /**
                Feed feed = new Feed.Builder()
                .setName("ShareFolio.net")
                .setCaption(company.companyName + "." + company.id)
                .setDescription("Company history to be shown here")
                .setPicture(companyAvatarLink)
                .setLink(companyLink)
                .build();
                **/


                Photo photo = new Photo.Builder()
                .setImage(screenShot)
                .setName("for testing only")
                .build();


                mSimpleFacebook.publish(photo, onPublishListener);

                Toast.makeText(CompanyActivity.this, "Share", Toast.LENGTH_SHORT).show();
            }
        });

        lvCompanyWall = (PullAndLoadListView)findViewById(R.id.lvCompanyWall);
        lvCompanyWall.addHeaderView(header);

        if (lvCompanyWall != null) {
            lvCompanyWall.setOnRefreshListener(this);
            lvCompanyWall.setOnLoadMoreListener(this);
        }

        new LoadCompanyAsyncTask().execute();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSimpleFacebook = SimpleFacebook.getInstance(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        mSimpleFacebook.onActivityResult(this, requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onLoadMore() {
        start += 15;
        new CompanyWallpostAsyncTask().execute();
    }

    @Override
    public void onRefresh() {
        start = 0;
        new CompanyWallpostAsyncTask().execute();
    }

    class LoadCompanyAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            for (History history: company.history) {
                cvChart.addDatum("chart_div", company.companyName, Float.parseFloat(history.value));
            }

            cvChart.drawCharts();

            tvCompanyName.setText(company.companyCode);
            tvCompanyPrice.setText(company.price.last);
            String priceChange = company.price.stockChange.equals("0.00") ? "(0.00%)" : 
                Common.percentageChange(Double.parseDouble(company.price.ref), Double.parseDouble(company.price.last));
            tvCompanyChange.setText((company.price.stockChange.equals("0.00") ? "0.000" : company.price.stockChange) + " " + priceChange);
            adapter = new WallAdapter(context, company.wallpost);
            lvCompanyWall.setAdapter(adapter);


            flProgress.setVisibility(View.GONE);

        }

        @Override
        protected Void doInBackground(Void... params) {
            CompanyApi api = new CompanyApi(context);
            try {
                company = api.getCompany(sm.getCurrentSession().ID, company.id, start, 15);
                company.history = api.getPriceChart(sm.getCurrentSession().ID, company.id);
            } catch (Exception e) {
                e.printStackTrace();
                company.history = new ArrayList<History>();
            }

            return null;
        }
    }

    class CompanyWallpostAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            adapter.walls = company.wallpost;
            adapter.notifyDataSetChanged();

            if (start == 0) {
                lvCompanyWall.onRefreshComplete();
            }
            else {
                lvCompanyWall.onLoadMoreComplete();
            }
        }

        @Override
        protected Void doInBackground(Void... params) {
            CompanyApi api = new CompanyApi(context);
            try {
                Company co = api.getCompany(sm.getCurrentSession().ID, company.id, start, 15);
                if (start == 0) {
                    company.wallpost = co.wallpost;
                }
                else {
                    company.wallpost.addAll(co.wallpost);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

    }
}

无法找到我在哪里犯错?

2 个答案:

答案 0 :(得分:0)

嗯,有两个潜在的问题:

  1. ChartView是一个自定义组件,它可能与“wrap_content”无法正常工作。 “wrap_content”指令依赖于窗口小部件能够正确报告其尺寸,因此可能存在问题。

  2. 另一个问题是你在cvChart下面的LinearLayout的高度设置为“match_parent”,这会使它重叠(因此遮挡)Chartview。您是否尝试将其更改为“wrap_content”并查看图表是否显示?

答案 1 :(得分:0)

LinearLayoutChartView的父级)身高也设为wrap_content

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

    <RelativeLayout
        android:id="@+id/rlForScreenShot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:layout_centerHorizontal="true" >

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

            <com.kite.share.chart.ChartView
                android:id="@+id/cvChart"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                />

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

                <TextView
                    android:id="@+id/tvCompanyName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Loading" />

                <TextView
                    android:id="@+id/tvCompanyPrice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:text="Loading" />

                <TextView
                    android:id="@+id/tvCompanyChange"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:text="Loading" />
            </LinearLayout>
            </LinearLayout> 
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rlCompanyInfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">"


        <ImageView
            android:id="@+id/ivCompanyLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/tvCompanyAbout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/ivCompanyLogo"
            android:text="txtEmpty" />

        <Button
            android:id="@+id/bShare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/tvCompanyAbout"
            android:text="Share" />

        <Button
            android:id="@+id/btnFollowToggle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/bShare"
            android:text="txtDummy" />
    </RelativeLayout>

</LinearLayout>

希望它适合你

变得像这样