作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
ScrollView 中有一个 ImageView ,其中包含当前可滚动的超长图像。我试图让这张图片自行滚动,但是 findViewById 在 ScrollView 上一直返回 null。
编辑:这是在 android wear 上
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ScrollView sv = (ScrollView) MainActivity.this.findViewById(R.id.mtr_scrollView);
//LinearLayout layout = (LinearLayout)sv.findViewById(R.id.something)
//sv.scrollTo(0, sv.getBottom());
sv.post(new Runnable() {
public void run() {
sv.fullScroll(View.FOCUS_DOWN);
//sv.scrollTo(0, sv.getBottom());
}
});
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTextView = (TextView) stub.findViewById(R.id.text);
}
});
handler = new Handler();
nodeListener = new NodeApi.NodeListener() {
@Override
public void onPeerConnected(Node node) {
remoteNodeId = node.getId();
handler.post(new Runnable() {
@Override
public void run() {
}
});
Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);
intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION);
intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.peer_connected));
startActivity(intent);
}
@Override
public void onPeerDisconnected(Node node) {
handler.post(new Runnable() {
@Override
public void run() {
}
});
Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);
intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.FAILURE_ANIMATION);
intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.peer_disconnected));
startActivity(intent);
}
};
messageListener = new MessageApi.MessageListener() {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
byte[]bytes = messageEvent.getData();
final String temp = new String(bytes);
if (messageEvent.getPath().equals(MESSAGE_PATH)) {
handler.post(new Runnable() {
@Override
public void run() {
}
});
}
}
};
apiClient = new GoogleApiClient.Builder(getApplicationContext()).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Wearable.NodeApi.addListener(apiClient, nodeListener);
Wearable.MessageApi.addListener(apiClient, messageListener);
Wearable.NodeApi.getConnectedNodes(apiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
if (getConnectedNodesResult.getStatus().isSuccess() && getConnectedNodesResult.getNodes().size() > 0) {
remoteNodeId = getConnectedNodesResult.getNodes().get(0).getId();
}
}
});
}
@Override
public void onConnectionSuspended(int i) {
}
}).addApi(Wearable.API).build();
}
我正在尝试滚动让 ScrollView 内的图像在应用程序启动后自动滚动。
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" tools:context=".MainActivity" tools:deviceIds="wear_round"
android:background="@color/black">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mtr_scrollView"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/something">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/sample"
android:id="@+id/imageView"
android:scaleType="fitStart"
android:layout_gravity="center_horizontal"
android:paddingTop="20dp"
android:paddingBottom="20dp" />
</LinearLayout>
</ScrollView>
最佳答案
查看您发布的 xml 问题不是 ScrollView,而是 WatchViewStub
。摆脱掉
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTextView = (TextView) stub.findViewById(R.id.text);
}
});
编辑
如果您使用的是 WatchViewStub
,则必须使用 onLayoutInflated
来初始化 View ,并且必须使用 stub
参数:
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
final ScrollView sv = (ScrollView) stub.findViewById(R.id.mtr_scrollView);
sv.post(new Runnable() {
public void run() {
sv.fullScroll(View.FOCUS_DOWN);
}
});
}
});
关于android - scrollview(findviewby id)返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30455562/
ScrollView 中有一个 ImageView ,其中包含当前可滚动的超长图像。我试图让这张图片自行滚动,但是 findViewById 在 ScrollView 上一直返回 null。 编辑:这
我是一名优秀的程序员,十分优秀!