我正在创建一个选项卡式布局作为导航抽屉活动的主页片段,
所以我在主要活动中调用名为“test”的片段作为默认值。喜欢
test home_fragment=new test();
android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.layout, home_fragment,home_fragment.getTag()).commit();
Run Code Online (Sandbox Code Playgroud)
如果我再次调用这个片段。喜欢
if (id == R.id.nav_home) {
test home_fragment=new test();
android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.layout, home_fragment,home_fragment.getTag()).commit();
}
Run Code Online (Sandbox Code Playgroud)
现在结果是 tab1 和 tab2 不见了
请参阅屏幕截图以获取更多参考
这是我的测试类(选项卡式布局)
package com.hackerinside.jaisonjoseph.testapp;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import static android.R.attr.x;
/**
* A simple {@link Fragment} subclass.
*/
public class test extends Fragment {
public SectionsPagerAdapter mSectionsPagerAdapter;
public ViewPager mViewPager;
public test() {
}
@Override …Run Code Online (Sandbox Code Playgroud) 我的主要活动包含 2 个片段:
主要活动
public class MainActivity extends AppCompatActivity implements FragmentOne.OnFragmentOneInteractionListener, FragmentTwo.OnFragmentTwoInteractionListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create …Run Code Online (Sandbox Code Playgroud) 这是我的代码,我无法在 Spinner 中添加项目。我不知道出了什么问题,也找不到任何其他方法!
爪哇:
spinner = (Spinner)getView().findViewById(R.id.spinner);
String[] datos = getResources().getStringArray(R.array.items);
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, datos);
adaptador.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adaptador);
Run Code Online (Sandbox Code Playgroud)
XML:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_margin="26dp"
android:textColor="#FFF"/>
Run Code Online (Sandbox Code Playgroud)
字符串.xml:
<string-array name="items">
<item >Item 1</item>
<item >Item 2</item>
<item >Item 3</item>
<item >Item 4</item>
</string-array>
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助。

MainActivity.java
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mBottomNavigationView;
String[] fragmentTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentTitles = getResources().getStringArray(R.array.bottom_nav);
mBottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
mBottomNavigationView.setOnChildClickedListener(new OnChildClickedListener() {
@Override
public void onChildClicked(int child) {
selectFragment(child);
}
});
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
if(savedInstanceState == null) {
selectFragment(0);
//getSupportActionBar().setTitle("Home");
}
}
private void selectFragment(int child) {
switch (child){
case 0:
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
.replace(R.id.content, HomeFragment.newInstance(fragmentTitles[child], child))
.commit();
break;
case 1:
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
.replace(R.id.content, SearchFragment.newInstance(fragmentTitles[child], child))
.commit();
break;
case 2: …Run Code Online (Sandbox Code Playgroud) 片段中的onKeyListener()现在不起作用.这是代码的一部分.
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, null);
...
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new View.OnKeyListener() {
// true if the listener has consumed the event, false otherwise.
// the key event happens twice, when pressing and taking off.
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && !touched) {
Log.i(TAG, "onKey() method");
touched = true;
return true;
} else {
return false;
}
}
});
return …Run Code Online (Sandbox Code Playgroud) 我的网站上有地理定位 - 我可以使用 javascript 获取位置,但我无法在 android 设备上使用 web 视图获取位置。我使用相同的网站,但它不起作用。
我的网站:https : //www.ardakazanci.thecompletewebhosting.com/petsHelper/paylasim.php
我的安卓代码:
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("https://www.ardakazanci.thecompletewebhosting.com/petsHelper/paylasim.php");
}
}
Run Code Online (Sandbox Code Playgroud)
它在网络上运行良好。不适用于 WebView。谢谢你。
错误:
地理定位服务失败
我的 javascript 代码 - paylasim.php
<script>
// Note: This example requires that you consent to location sharing when
// prompted by your browser. If you see the error "The Geolocation service
// failed.", it means you probably did not …Run Code Online (Sandbox Code Playgroud) 我正在制作应用程序实现ViewPager和标签视图,但标题部分是空白的.请解释如何为每个设置标题fragments.
我的MainActivity.java
package com.example.android.viewpager;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
/**
* Displays a {@link ViewPager} where each page shows a different day of the week.
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
// Find the view pager that will allow the user to swipe between fragments
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
// …Run Code Online (Sandbox Code Playgroud) 我试图使3个选项卡出现在ImageView的底部,并且最好将其重叠,以便当您向下滚动ViewPager时仍可以看到这些选项卡。
我遇到的问题是选项卡在ImageView上方,并被推离屏幕,因此当您向下滚动时看不到它们。
这是我的XML布局:
<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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true"
tools:context="com.example.aine.cardview.MainActivityTabbed">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?android:attr/colorControlHighlight"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
Run Code Online (Sandbox Code Playgroud)
我对TabLayouts的工作方式不太熟悉,所以我不太确定如何解决此问题。
在一种情况下,我在内部创建了线性布局HorizontalScrollView ,问题是我无法禁用我尝试过的滚动条scrollbarstyle;scrollbarsize,setHorizontalScrollbarEnabled但这些方法都不起作用?
我的 xml 代码是:-
<HorizontalScrollView
android:id="@+id/horizontal_times"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/divider_one"
android:layout_marginTop="8dp">
<LinearLayout
android:id="@+id/layout_times"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="16dp" />
</HorizontalScrollView>`
Run Code Online (Sandbox Code Playgroud) 我希望在图像中使用与下面的布局完全相似的布局.我已经尝试了下面的代码,但它没有来.有人可以帮助我获得这种布局.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Ali Connors"
android:textSize="12sp" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/firstLine"
android:gravity="center_vertical"
android:text="Brunch this weekend?"
android:textSize="16sp" />
<TextView
android:gravity="right"
android:id="@+id/rightTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="15m"
android:textSize="16sp" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud) 我首先创建Android Test App。
我使用的是Webview表单,并插入了一些网址,当我运行应用程序时,它的工作正常。
但是,当我打开应用程序并单击其他页面链接时,例如,我没有选择列表,请在“ Google Chrome”上将其打开。但是我想在webview中打开它。
我认为这一点很清楚。
activity_mail.xml
<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" android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp" tools:context=".MainActivity">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
package com.example.webviewapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://example.com";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我已经查看了其他答案,但没有一个能帮助我.我确保使用import android.support.v4.app.Fragment;,这不是问题.
MainActivity.java
package com.example.nirvan.fragmentsexample2;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFragment myfragment=new myFragment();
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer,myfragment);
fragmentTransaction.commit();
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
Error:(20, 28) error: no suitable method found for add(int,myFragment)
method FragmentTransaction.add(Fragment,String) is not applicable
(argument mismatch; int cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment) is not applicable
(argument mismatch; myFragment cannot be converted to Fragment)
Run Code Online (Sandbox Code Playgroud)
我为什么要这个?
在activity_main.xml中 …
android ×12
java ×2
webview ×2
geolocation ×1
google-maps ×1
items ×1
javascript ×1
spinner ×1
string ×1
xml ×1