jjzjj

android - 工具栏不会在创建时或在 XML 中设置高程

coder 2023-12-20 原文

我目前正在为学校开发 Android 应用程序,但仍想尽我所能。总的来说,我对 Android 开发和编码还很陌生。 该应用程序应该是一个股票市场游戏。 (顺便说一句,我是德国人,所以可能会有一些德国变量) 该应用程序的风格应该与谷歌最近推出的 Pixel 2 和 Android Pie 相似。因此,工具栏在创建时应该没有阴影,但在滚动时应该出现,就像在 Pixel 2 的设置应用程序中一样(例如电池选项卡)。 滚动时投影出现,到达顶部时消失,但工具栏以投影开始,尽管我在 XML 中将 android:elevationtoolbar.setElevation 设置为 0 (0)onCreate 方法中。 为什么会这样?此方法适用于 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 中设置高程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51945569/

有关android - 工具栏不会在创建时或在 XML 中设置高程的更多相关文章

  1. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  2. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  3. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  4. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  5. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  6. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

  7. ruby - 如何使用 RSpec::Core::RakeTask 创建 RSpec Rake 任务? - 2

    如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake

  8. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  9. ruby - 为什么 SecureRandom.uuid 创建一个唯一的字符串? - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?

  10. ruby - 有人可以帮助解释类创建的 post_initialize 回调吗 (Sandi Metz) - 2

    我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法

随机推荐