Android Wear 2.0上的曲线布局

时间:2017-06-23 13:19:30

标签: android wear-os

我试图创建一个弯曲的布局来列出一些元素并使其适应机器人的磨损,但关于它的谷歌Android开发者页面目前还不清楚。有没有人设法创建一个弯曲的布局?

这是我正在谈论的Google Developers:

https://developer.android.com/training/wearables/ui/lists.html#creating

如果您有任何提示可以实现它,我很高兴看到它。

1 个答案:

答案 0 :(得分:2)

我希望你现在想出来,但如果没有,这里是一些示例代码的步骤:

round\activity_main.xml使用android.support.wear.widget.WearableRecyclerView作为根/主要元素:

<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.WearableRecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rv_test"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<!-- ... -->

</android.support.wear.widget.WearableRecyclerView>

然后在MainActivity扩展WearableActivity

import android.os.Bundle;
import android.support.wearable.activity.WearableActivity;
import android.support.wear.widget.WearableRecyclerView

public class MainActivity extends WearableActivity {

    private android.support.wear.widget.WearableRecyclerView wearableRecyclerView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        wearableRecyclerView = findViewById(R.id.rv_test);
        wearableRecyclerView.setLayoutManager(new WearableLinearLayoutManager(MainActivity.this));
        wearableRecyclerView.setEdgeItemsCenteringEnabled(true);
    }

    ...
}

您需要确保在xml和java上使用WearableRecyclerViewandroid.support.wear.widget.WearableRecyclerView)的正确导入

下一步(开发人员网页中未解释的是创建ViewHolder

1 - 首先创建一个xml布局(此处称为circle_view.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/close_button"
        android:id="@+id/circledImageView"/>

</LinearLayout>

2 - 然后创建一个java类(此处称为TestHolder.java)并将其扩展为RecyclerView.ViewHolder

public class TestHolder extends RecyclerView.ViewHolder {
                protected ImageView imageView;
                public TestHolder(View itemView) {
                    super(itemView);
                    imageView = (ImageView) itemView.findViewById(R.id.circledImageView);
                }
            }

然后返回MainActivity.java,在wearableRecyclerView.setEdgeItemsCenteringEnabled(true);内的onCreate下方添加以下内容:

RecyclerView.Adapter<TestHolder> testHolderAdapter = new RecyclerView.Adapter<TestHolder>() {
            @Override
            public TestHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.circle_view, parent, false);
                return new TestHolder(view);
            }

            @Override
            public void onBindViewHolder(TestHolder holder, int position) {

            }

            @Override
            public int getItemCount() {
                //change return number to desired number of items or an array size or list length
                return 5;
            }
        };
        wearableRecyclerView.setAdapter(testHolderAdapter);

这就是全部,在圆形磨损模拟器或圆形/圆形下巴手表上运行您的应用程序并享受曲线列表