【问题标题】:XML file wrap_content issueXML 文件 wrap_content 问题
【发布时间】:2014-10-01 05:08:24
【问题描述】:

我有这个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 尝试过,但对我也不起作用。无论如何我可以解决这个问题吗?

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

@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;
        }

    }
}

找不到我犯错的地方吗?

【问题讨论】:

  • 您能否发布一些示例图片作为您的活动的样子?
  • Oam,我无法发布图像,因为我还没有达到发布图像的声誉配额。但基本上,图表在运行时会重叠,但我为高度设置了一个固定数字,它显示得很好。

标签: java android xml android-layout


【解决方案1】:

嗯,有两个潜在的问题:

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

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

【讨论】:

  • 嗨,迈克,试过了。它没有用。有没有办法在java中动态设置chartView高度属性为“wrap_content”? Bcos d chartView 是一个java文件对象。
【解决方案2】:

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>

希望对你有用

变成这样

【讨论】:

  • MS Gadag,LinearLayout 已设置为 ChartView 的父级。
  • 图表视图不显示。它是重叠的。我试过你的代码。
  • 你能上传所需布局的图片吗?
  • 我无法上传图片,因为我还没有达到 stackoverflow 的信誉点配额。 bt 你的图像正是我想要实现的,这使得它重叠的原因令人困惑。
  • 好的,我将粘贴所有代码,只需删除所有 xml 代码并粘贴
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
  • 2020-05-14
  • 2017-08-08
  • 2010-11-13
  • 1970-01-01
相关资源
最近更新 更多