英文:
Can't see view items in fragment
问题
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<TextView
android:id="@+id/infos"
android:layout_width="197dp"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="-5dp"
android:layout_marginTop="-4dp"
android:layout_marginEnd="219dp"
android:layout_marginBottom="634dp"
android:text="@string/resInfos"
android:textSize="30sp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="686dp"
android:contentDescription="@string/cnt"
app:srcCompat="@drawable/pas_info" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
...
android:background="@color/colorPrimaryDark"
android:text="@string/resTitreInformations"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="visible" />
<Button
android:id="@+id/endButton"
android:layout_width="288dp"
android:layout_height="38dp"
...
android:onClick="terminer"
android:text="@string/resBoutonFin"
android:textColor="@android:color/holo_red_dark"
android:textStyle="bold"
android:visibility="visible" />
</RelativeLayout>
public class InfosFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_infos, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Button bouton = (Button) getView().findViewById(R.id.endButton);
bouton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
terminer();
}
});
}
public void terminer() {
System.exit(0);
}
}
package com.example.vlcapplicationvendeur;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.MenuItem;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNv = findViewById(R.id.bottom_nav);
bottomNv.setOnNavigationItemSelectedListener(navListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.nav_infos:
selectedFragment = new InfosFragment();
break;
case R.id.nav_avis:
selectedFragment = new AvisFragment();
break;
case R.id.nav_tendance:
selectedFragment = new TendanceFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
return true;
}
};
}
[what I see][1]
[what I want to see][2]
<details>
<summary>英文:</summary>
I am new to Android Studios and I am working on an application using fragments.
My code is ok and the build succed but the main fragment don't show some textViews and Buttons declared in InfosFragment.xml. MainActivity can switch between fragments with a container.
I don't know what to try.
**Fragment_infos.xml**
``` <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<TextView
android:id="@+id/infos"
android:layout_width="197dp"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="-5dp"
android:layout_marginTop="-4dp"
android:layout_marginEnd="219dp"
android:layout_marginBottom="634dp"
android:text="@string/resInfos"
android:textSize="30sp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="686dp"
android:contentDescription="@string/cnt"
app:srcCompat="@drawable/pas_info" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
...
android:background="@color/colorPrimaryDark"
android:text="@string/resTitreInformations"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="visible" />
<Button
android:id="@+id/endButton"
android:layout_width="288dp"
android:layout_height="38dp"
...
android:onClick="terminer"
android:text="@string/resBoutonFin"
android:textColor="@android:color/holo_red_dark"
android:textStyle="bold"
android:visibility="visible" />
```
**infosFragment.java**
``` public class InfosFragment extends Fragment
{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_infos, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
{
Button bouton = (Button) getView().findViewById(R.id.endButton);
bouton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
terminer();
}
});
}
public void terminer()
{
System.exit(0);
}
} ```
**MainActivity.java**
``` package com.example.vlcapplicationvendeur;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.MenuItem;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*if(savedInstanceState.isEmpty())
{
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}*/
// création du switcher entre deux fragments
BottomNavigationView bottomNv = findViewById(R.id.bottom_nav);
bottomNv.setOnNavigationItemSelectedListener(navListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem)
{
Fragment selectedFragment = null;
switch (menuItem.getItemId())
{
case R.id.nav_infos:
selectedFragment = new InfosFragment();
break;
case R.id.nav_avis:
selectedFragment = new AvisFragment();
break;
case R.id.nav_tendance:
selectedFragment = new TendanceFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
return true;
}
};
} ```
[what I see][1]
[what I want to see][2]
[1]: https://i.stack.imgur.com/mYNCO.png
[2]: https://i.stack.imgur.com/JCfIH.png
</details>
# 答案1
**得分**: 0
我认为你的布局没有显示出来,可能是因为你在``TextView``中定义了一些属性:
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
你可以尝试使用``LinearLayout``,然后再考虑使用``RelativeLayout``。
<details>
<summary>英文:</summary>
I think your Layout is not showning because of some attributes you defined in your ``TextView``:
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
You can try ``LinearLayout`` instead and then move to ``RelativeLayout``.
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论