【发布时间】:2016-04-30 03:52:52
【问题描述】:
我是 android wear 开发的菜鸟,到目前为止一切都很好。 我已经成功创建了我的第一个 hello world 应用并在我的 LG G watch R 上运行它。
我遇到的问题如下; 我正在使用开发人员站点中描述的 WatchViewStub,WatchViewStub 应该检测应用程序正在运行的屏幕类型。 但显然 WatchViewStub 已经完成了它应该做的一半,这意味着应用程序使用正确的 xml 文件“round_activity”和正确的文本进行部署,但它仍然在设备上以方形布局显示。
据我了解,WatchViewStub 应该在检测到圆形显示时自动修复文本。但在我的情况下,它从正确的 round_activity xml 文件中获取正确的文本,但它以方形格式显示。 这是我的代码:
主活动:
public class MainActivity extends Activity {
public TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override public void onLayoutInflated(WatchViewStub stub) {
// Now you can access your views
mTextView = (TextView) stub.findViewById(R.id.text);
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
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/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/rect_activity_main"
app:roundLayout="@layout/round_activity_main"
tools:context=".MainActivity"
tools:deviceIds="wear">
</android.support.wearable.view.WatchViewStub>
round_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
tools:deviceIds="wear_square">
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_round" />
</RelativeLayout>
square_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
tools:deviceIds="wear_square">
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_square" />
</RelativeLayout>
让我的 hello round world 文本正确显示的唯一方法是将其填充到 xml 的中心。
有人知道为什么会这样吗? 提前感谢各位大侠的帮助
【问题讨论】:
-
两种布局看起来一样 - 目前唯一的区别是分配给您的 TextView 的字符串资源。