android-工具栏未在创建或XML中设置高程

android-工具栏未在创建或XML中设置高程,第1张

概述我目前正在为学校创建一个Android应用程序,但仍然想尽我所能.总的来说,我对Android开发和编码还很陌生.该应用程序应该是股票市场游戏.(顺便说一句,我是德语,所以可能会有一些德语变量)该应用应该具有与Google最近的Pixel2和AndroidPie相似的风格.因此,工具栏在创建时应该没有阴

我目前正在为学校创建一个Android应用程序,但仍然想尽我所能.总的来说,我对AndroID开发和编码还很陌生.
该应用程序应该是股票市场游戏.
(顺便说一句,我是德语,所以可能会有一些德语变量)
该应用应该具有与Google最近的Pixel 2和AndroID PIE相似的风格.因此,工具栏在创建时应该没有阴影,但在滚动时应该出现,就像在Pixel 2的设置应用程序中(例如“电池”标签)一样.
阴影在滚动时出现,并在到达顶部时消失,但是工具栏以阴影开始,尽管我在XML中将androID:elevation设置为0,并且在onCreate方法中将toolbar.setElevation(0)设置为.
为什么会这样?此方法可在OnScrollChangedListener中使用!

Java代码:

import androID.content.Context;import androID.content.Intent;import androID.content.SharedPreferences;import androID.os.Bundle;import androID.support.design.Widget.AppbarLayout;import androID.support.design.Widget.floatingActionbutton;import androID.support.v4.Widget.nestedScrollVIEw;import androID.support.v4.Widget.SwipeRefreshLayout;import androID.support.v7.app.AppCompatActivity;import androID.support.v7.Widget.DefaultItemAnimator;import androID.support.v7.Widget.RecyclerVIEw;import androID.support.v7.Widget.Toolbar;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.vIEw.VIEwTreeObserver;import androID.Widget.ScrollVIEw;import androID.Widget.TextVIEw;import androID.Widget.Toast;import org.Json.JsONException;import org.Json.JsONObject;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {    ArrayList<JsONObject> jObjList = new ArrayList<>();    private floatingActionbutton fab;    private TextVIEw moneyTxt;    private TextVIEw sharesTxt;    private TextVIEw sumTxt;    private Toolbar toolbar;    private AppbarLayout toolbarLayout;    private RecyclerVIEw recyclerShares;    private SharesAdapter sAdapter;    private nestedScrollVIEw scrollMain;    private SwipeRefreshLayout refreshMain;    private float money;    private float sharesWorth;    private boolean isRefreshing;    private JsONObject jObj = new JsONObject();    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        toolbar = findVIEwByID(R.ID.abMain);        toolbar.setTitleTextcolor(getResources().getcolor(R.color.colorPrimary));        setSupportActionbar(toolbar);        fab = findVIEwByID(R.ID.fabMain);        moneyTxt = findVIEwByID(R.ID.moneyTxt);        sharesTxt = findVIEwByID(R.ID.sharesTxt);        sumTxt = findVIEwByID(R.ID.sumTxt);        toolbarLayout = findVIEwByID(R.ID.abMainLayout);        recyclerShares = findVIEwByID(R.ID.recyclerShares);        scrollMain = findVIEwByID(R.ID.scrollMain);        scrollMain.setDescendantFocusability(VIEwGroup.FOCUS_BEFORE_DESCENDANTS);        refreshMain = findVIEwByID(R.ID.refreshMain);        isRefreshing = false;        try {            jObj.put("name", "BMW");            jObj.put("worth", 143.23);            jObj.put("count", 5);            jObj.put("change", -1.5);            jObjList.add(jObj);        } catch (JsONException e) {        }        sAdapter = new SharesAdapter(jObjList);        RecyclerVIEw.LayoutManager cLayoutManager = new CustomGrIDLayoutManager(getApplicationContext()) {            @OverrIDe            public boolean canScrollVertically() {                return false;            }        };        recyclerShares.setLayoutManager(cLayoutManager);        recyclerShares.setItemAnimator(new DefaultItemAnimator());        recyclerShares.setAdapter(sAdapter);        fab.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                Intent intent = new Intent(MainActivity.this, SharesActivity.class);                startActivity(intent);            }        });        toolbarLayout.setElevation(0); //Todo: Stackoverflow nach Lösung fragen        scrollMain.getVIEwTreeObserver().addOnScrollChangedListener(new VIEwTreeObserver.OnScrollChangedListener() {            @OverrIDe            public voID onScrollChanged() {                int scroll = scrollMain.getScrollY();                if (scroll == 0) {                    toolbarLayout.setElevation(0);                } else {                    toolbarLayout.setElevation(8);                }            }        });        refreshMain.setonRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {            @OverrIDe            public voID onRefresh() {                refresh(MainActivity.this);            }        });        refresh(MainActivity.this);    }    private voID refresh(Context context) {        isRefreshing = true;        SharedPreferences sharedPref = context.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);        SharedPreferences.Editor sharedPrefEdit = sharedPref.edit();        if (sharedPref.getBoolean("isFirstRun", true)) {            sharedPrefEdit.putBoolean("isFirstRun", false);            sharedPrefEdit.putfloat(getString(R.string.moneyShared), 5000);            sharedPrefEdit.apply();        }        float shareWorth = 0;        for (int i = 0; i < jObjList.size(); i++) {            try {                shareWorth += jObjList.get(i).getDouble("worth") * jObjList.get(i).getDouble("count");            } catch (JsONException e) {            }        }        sharedPrefEdit.putfloat(getString(R.string.sharesWorthShared), shareWorth);        sharedPrefEdit.commit();        money = sharedPref.getfloat(getString(R.string.moneyShared), 0);        sharesWorth = sharedPref.getfloat(getString(R.string.sharesWorthShared), 0);        moneyTxt.setText(String.format("%.2f€", money));        sharesTxt.setText(String.format("%.2f€", sharesWorth));        sumTxt.setText(String.format("%.2f€", money + sharesWorth));        if(isRefreshing) {            isRefreshing = false;            refreshMain.setRefreshing(isRefreshing);        }        Toast.makeText(MainActivity.this, "Alles neugeladen", Toast.LENGTH_SHORT).show();    }    public voID onShareClick(VIEw v) {        Intent i = new Intent(MainActivity.this, CompanyActivity.class);        try {            i.putExtra("name", jObj.getString("name"));            i.putExtra("worth", jObj.getDouble("worth"));        } catch (JsONException e) {            i.putExtra("name", "Fehler");            i.putExtra("worth", 0);        }        startActivity(i);    }}

XML代码:

<?xml version="1.0" enCoding="utf-8"?><androID.support.constraint.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:ID="@+ID/container"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:background="@color/colorBackground"    tools:context=".MainActivity">    <androID.support.design.Widget.AppbarLayout        androID:ID="@+ID/abMainLayout"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:elevation="0dp">        <androID.support.v7.Widget.Toolbar            androID:ID="@+ID/abMain"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:background="@color/colorBackgroundAccent"            androID:theme="@style/themeOverlay.AppCompat.Actionbar"            app:popuptheme="@style/themeOverlay.AppCompat.light" />    </androID.support.design.Widget.AppbarLayout>    <androID.support.design.Widget.floatingActionbutton        androID:ID="@+ID/fabMain"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_gravity="end|bottom"        androID:layout_margin="16dp"        androID:layout_marginBottom="8dp"        androID:layout_marginEnd="8dp"        androID:backgroundTint="@color/colorBackground"        androID:src="@drawable/ic_note_add"        app:borderWIDth="0dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent" />    <androID.support.v4.Widget.SwipeRefreshLayout        androID:ID="@+ID/refreshMain"        androID:layout_wIDth="0dp"        androID:layout_height="0dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_toBottomOf="@+ID/abMainLayout">        <androID.support.v4.Widget.nestedScrollVIEw            androID:ID="@+ID/scrollMain"            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:focusableIntouchMode="true"            app:layout_constrainttop_toBottomOf="@+ID/abMainLayout">            <linearLayout                androID:layout_wIDth="match_parent"                androID:layout_height="wrap_content"                androID:orIEntation="vertical">                <GrIDLayout                    androID:ID="@+ID/grIDMoney"                    androID:layout_wIDth="match_parent"                    androID:layout_height="match_parent"                    androID:background="@color/colorBackgroundAccent"                    androID:orIEntation="horizontal"                    androID:paddingBottom="@dimen/activity_vertical_margin"                    androID:paddingtop="24dp"                    app:layout_constraintEnd_toEndOf="parent"                    app:layout_constraintStart_toStartOf="parent"                    app:layout_constrainttop_toBottomOf="@ID/abMain">                    <TextVIEw                        androID:ID="@+ID/sumTxt"                        androID:layout_wIDth="wrap_content"                        androID:layout_height="wrap_content"                        androID:layout_columnSpan="5"                        androID:layout_columnWeight="1"                        androID:layout_gravity="center"                        androID:layout_marginBottom="16dp"                        androID:layout_margintop="8dp"                        androID:layout_row="1"                        androID:textcolor="@color/colorDarkText"                        androID:textSize="50sp"                        androID:textStyle="bold" />                    <ImageVIEw                        androID:ID="@+ID/moneyimg"                        androID:layout_wIDth="wrap_content"                        androID:layout_height="wrap_content"                        androID:layout_column="1"                        androID:layout_columnWeight="1"                        androID:layout_gravity="right|center_vertical"                        androID:layout_row="2"                        androID:background="@androID:color/transparent"                        androID:scaleX="0.5"                        androID:scaleY="0.5"                        app:srcCompat="@drawable/ic_money" />                    <TextVIEw                        androID:ID="@+ID/moneyTxt"                        androID:layout_wIDth="wrap_content"                        androID:layout_height="wrap_content"                        androID:layout_column="2"                        androID:layout_columnWeight="1"                        androID:layout_gravity="left|center_vertical"                        androID:layout_row="2"                        androID:background="@androID:color/transparent"                        androID:textcolor="@color/colorlightText"                        androID:textSize="15sp" />                    <ImageVIEw                        androID:ID="@+ID/sharesimg"                        androID:layout_wIDth="wrap_content"                        androID:layout_height="wrap_content"                        androID:layout_column="3"                        androID:layout_columnWeight="1"                        androID:layout_gravity="right|center_vertical"                        androID:layout_row="2"                        androID:background="@androID:color/transparent"                        androID:scaleX="0.5"                        androID:scaleY="0.5"                        app:srcCompat="@drawable/outline_assessment_black_36" />                    <TextVIEw                        androID:ID="@+ID/sharesTxt"                        androID:layout_wIDth="wrap_content"                        androID:layout_height="wrap_content"                        androID:layout_column="4"                        androID:layout_columnWeight="1"                        androID:layout_gravity="left|center_vertical"                        androID:layout_row="2"                        androID:background="@androID:color/transparent"                        androID:textcolor="@color/colorlightText"                        androID:textSize="15sp" />                </GrIDLayout>                <VIEw                    androID:ID="@+ID/divIDerMoney"                    androID:layout_wIDth="match_parent"                    androID:layout_height="1dp"                    androID:background="@color/colordivIDer"                    app:layout_constrainttop_toBottomOf="@ID/grIDMoney" />                <TextVIEw                    androID:layout_wIDth="match_parent"                    androID:layout_height="wrap_content"                    androID:layout_marginEnd="@dimen/recycler_horizontal_margin"                    androID:layout_marginStart="@dimen/recycler_horizontal_margin"                    androID:layout_margintop="@dimen/activity_vertical_margin"                    androID:text="AktIEn"                    androID:textcolor="@color/colorPrimary"                    androID:textStyle="bold"                    app:layout_constraintEnd_toEndOf="@+ID/divIDerMoney"                    app:layout_constraintStart_toStartOf="parent"                    app:layout_constrainttop_toBottomOf="@+ID/grIDMoney" />                <androID.support.v7.Widget.RecyclerVIEw                    androID:ID="@+ID/recyclerShares"                    androID:layout_wIDth="match_parent"                    androID:layout_height="match_parent"                    androID:layout_margintop="@dimen/recycler_Title_bottom_margin"                    app:layout_constraintEnd_toEndOf="parent"                    app:layout_constraintStart_toStartOf="parent"                    app:layout_constrainttop_toBottomOf="@+ID/textVIEw" />            </linearLayout>        </androID.support.v4.Widget.nestedScrollVIEw>    </androID.support.v4.Widget.SwipeRefreshLayout></androID.support.constraint.ConstraintLayout>

解决方法:

所以@NileshRathod回答了它:

它是app:elevation,而不是androID:elevation.
但是我仍然不明白为什么onCreate()中的方法不起作用.

总结

以上是内存溢出为你收集整理的android-工具栏未在创建或XML中设置高程全部内容,希望文章能够帮你解决android-工具栏未在创建或XML中设置高程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1210097.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存