看不到片段中的视图项目

huangapple 未分类评论47阅读模式
英文:

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&#39;t show some textViews and Buttons declared in InfosFragment.xml. MainActivity can switch between fragments with a container.
I don&#39;t know what to try.


**Fragment_infos.xml**

    ```    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
        xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:background=&quot;@android:color/white&quot;&gt;
    
        &lt;TextView
            android:id=&quot;@+id/infos&quot;
            android:layout_width=&quot;197dp&quot;
            android:layout_height=&quot;100dp&quot;
            android:layout_alignParentStart=&quot;true&quot;
            android:layout_alignParentTop=&quot;true&quot;
            android:layout_alignParentEnd=&quot;true&quot;
            android:layout_alignParentBottom=&quot;true&quot;
            android:layout_marginStart=&quot;-5dp&quot;
            android:layout_marginTop=&quot;-4dp&quot;
            android:layout_marginEnd=&quot;219dp&quot;
    
            android:layout_marginBottom=&quot;634dp&quot;
            android:text=&quot;@string/resInfos&quot;
            android:textSize=&quot;30sp&quot; /&gt;
    
        &lt;ImageView
            android:id=&quot;@+id/imageView2&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;686dp&quot;
            android:contentDescription=&quot;@string/cnt&quot;
            app:srcCompat=&quot;@drawable/pas_info&quot; /&gt;
    
        &lt;TextView
            android:id=&quot;@+id/textView&quot;
            android:layout_width=&quot;wrap_content&quot;
            ...
            android:background=&quot;@color/colorPrimaryDark&quot;
            android:text=&quot;@string/resTitreInformations&quot;
            android:textSize=&quot;24sp&quot;
            android:textStyle=&quot;bold&quot;
            android:visibility=&quot;visible&quot; /&gt;
    
        &lt;Button
            android:id=&quot;@+id/endButton&quot;
            android:layout_width=&quot;288dp&quot;
            android:layout_height=&quot;38dp&quot;
            ...
            android:onClick=&quot;terminer&quot;
            android:text=&quot;@string/resBoutonFin&quot;
            android:textColor=&quot;@android:color/holo_red_dark&quot;
            android:textStyle=&quot;bold&quot;
            android:visibility=&quot;visible&quot; /&gt;    
    ```

**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&#233;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>



huangapple
  • 本文由 发表于 2020年4月8日 01:17:12
  • 转载请务必保留本文链接:https://java.coder-hub.com/61085661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定