如果我在应用程序运行时使用setContentView(R.layout.activity_main);
向我显示工具栏但我无法操作content_main中的字段,但如果我更改为setContentView(R.layout.content_main);
字段有效但工具栏会消失。
我能做些什么来解决这个问题?
主要
public class MainActivity extends AppCompatActivity {
int contador;
boolean podeSwipe = true;
ImageView photoImageViewBotton;
ImageView photoImageViewTop;
TextView nameTextbox;
TextView descriptionTextbox;
ConstraintLayout mainLayout;
int[] listaImagens = {R.drawable.sheldon01, R.drawable.sheldon02, R.drawable.sheldon03, R.drawable.sheldon04, R.drawable.sheldon05};
@Override
protected void onCreate(Bundle savedInstanceState) {
// Apontando o layout
super.onCreate(savedInstanceState);
(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
photoImageViewTop = (ImageView) findViewById(R.id.photoImageViewTop);
photoImageViewBotton = (ImageView) findViewById(R.id.photoImageViewBotton);
nameTextbox = (TextView) findViewById(R.id.nameTextbox);
descriptionTextbox = (TextView) findViewById(R.id.descriptionTextbox);
mainLayout = (ConstraintLayout) findViewById(R.id.mainLayout);
nameTextbox.setText("Sheldon,");
descriptionTextbox.setTag(descriptionTextbox.getKeyListener());
descriptionTextbox.setKeyListener(null);
descriptionTextbox.setText("123456789" +
"sdfsdfsdfs\n" +
"sdfsdfsdfs\n" +
"sdfsdfsdfs\n" +
"sdfsdfsdfs\n" +
"sdfsdfsdfs\n" +
"sdfsdfsdfs\n" +"sdfsdfsdfs\n" +"sdfsdfsdfs\n" +"sdfsdfsdfs\n" +"sdfsdfsdfs\n" +"sdfsdfsdfs\n" +
"dfsdfsdfsdfdsf\n" +"dfsdfsdfsdfdsf\n" +"dfsdfsdfsdfdsf\n" +"dfsdfsdfsdfdsf\n" +"dfsdfsdfsdfdsf\n" +"dfsdfsdfsdfdsf\n" +
"dfsdfsdfsdfsdfsdfsdfsdf\n");
photoImageViewBotton.setImageResource(R.drawable.sheldon02);
photoImageViewTop.setImageResource(R.drawable.sheldon01);
photoImageViewBotton.setAlpha(0.0f);
photoImageViewBotton.setRotation(-30.0f);
photoImageViewBotton.setScaleX(0.35f);
photoImageViewBotton.setScaleY(0.35f);
photoImageViewTop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changePhoto();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
活动
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="zuch.co.a4aces.MainActivity">
<include
android:id="@+id/include"
layout="@layout/content_main" />
<include layout="@layout/content_main" />
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:theme="@style/AppTheme.AppBarOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<ImageButton
android:id="@+id/configurationButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="8dp"
android:layout_marginTop="0dp"
android:background="@android:color/background_light"
android:tint="@android:color/holo_purple"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@android:drawable/ic_menu_edit" />
<ImageButton
android:id="@+id/chatButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="0dp"
android:background="@android:color/background_light"
android:tint="@color/Purple"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintLeft_toRightOf="@+id/imageView2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@android:drawable/sym_action_chat" />
<ImageButton
android:id="@+id/mainButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:color/background_light"
android:scaleType="fitXY"
app:srcCompat="@drawable/logo" />
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
内容
<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/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="zuch.co.a4aces.MainActivity"
tools:showIn="@layout/activity_main">
<RelativeLayout
android:id="@+id/descriptionLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="0dp"
android:layout_marginTop="0dp"
app:layout_constraintBottom_toTopOf="@+id/buttonRelactiveLayout"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/photoImageViewTop"
app:layout_constraintVertical_bias="0.0">
<EditText
android:id="@+id/descriptionTextbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:elevation="1dp"
android:ems="10"
android:inputType="textMultiLine" />
</RelativeLayout>
<EditText
android:id="@+id/nameTextbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:editable="false"
android:ems="10"
android:inputType="none"
android:text="Name"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/photoImageViewBotton"
android:layout_width="0dp"
android:layout_height="200dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nameTextbox"
app:srcCompat="@drawable/sheldon03" />
<ImageView
android:id="@+id/photoImageViewTop"
android:layout_width="0dp"
android:layout_height="200dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nameTextbox"
app:srcCompat="@drawable/sheldon03" />
<RelativeLayout
android:id="@+id/buttonRelactiveLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="false"
android:background="@android:color/holo_green_light"
android:tint="@android:color/background_light"
app:srcCompat="@drawable/heart" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_blue_dark"
app:srcCompat="@drawable/star01"
tools:layout_editor_absoluteX="163dp"
tools:layout_editor_absoluteY="457dp" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@android:color/holo_red_dark"
android:tint="@android:color/background_light"
app:srcCompat="@android:drawable/ic_delete"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="2dp" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
使用setContentView(R.layout.activity_main);
并访问content_view
的元素
View contentView = findViewById(R.id.id_content_main);
并访问
等元素TextView tv=(TextView)contentView.findViewByID(R.id.some_id);
tv.setText("Anything");