[英]Changing ImageView size
I'm trying to have an image show up when the name of it (TextVIew) is clicked. 我正在尝试单击它的名称(TextVIew)时显示图像。 I have the code set up to what I believe is the right thing but the app force closes when I launch it. 我已经将代码设置为我认为正确的设置,但是当我启动它时,应用程序会关闭。 This is just a test code, so its not my actual app, but the idea is the same so can someone help me figure out why this is crashing? 这只是一个测试代码,所以不是我的实际应用程序,但是想法是一样的,所以有人可以帮我弄清楚为什么这会崩溃吗?
java file: Java文件:
TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
image.getLayoutParams().height=20;
}
});
}
xml file: xml文件:
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:gravity="center"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:text="show image" />
You have to have this code (below) inside onCreate()
您必须在onCreate()
包含此代码(如下onCreate()
TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
Try it. 试试吧。
Update: 更新:
The code could look like this: 代码看起来像这样:
public class MainActivity extends Activity {
ImageView image = null;
TextView text = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
image = (ImageView) findViewById(R.id.image);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (image.getVisibility() == View.GONE) {
image.setVisibility(View.VISIBLE);
text.setText("hide the image");
} else {
image.setVisibility(View.GONE);
text.setText("show the image");
}
}
});
}
}
and layout is here: 布局在这里:
<LinearLayout 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:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:visibility="gone" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show the image" />
</LinearLayout>
Fill your elements in onCreate
, not as a field, after setting the content view; 设置内容视图后,将元素填充到onCreate
而不是字段中;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
...
And try setting the size of the ImageView
not directly from layoutparams but with setHeight/setWidth
functions 并尝试不直接从layoutparams而是使用setHeight/setWidth
函数设置ImageView
的大小
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.