我尝试了新的Android支持设计库中的navigationView。我想要一个动态的headerview。基本上,我的标题视图将显示当天的报价。我喜欢大约10个引号,我想随机选择一个引用并在headerView的textview中显示。我还想为headerView添加onClick方法。
目前,我还没有看到以编程方式更改标题视图布局的任何可能性。有任何建议可以实现吗?
答案 0 :(得分:24)
首先创建标题XML,如 lay_header.xml
<TextView
android:id="@+id/tvThought"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
你的java文件上的在TextView中膨胀了上面的标题。像
TextView headerView = (TextView) LayoutInflater.from(this).inflate(R.layout.lay_header, null);
headerView.setText("Your_thoght");
现在将其添加为HeaderView
navView = (NavigationView) findViewById(R.id.navView);
navView.addHeaderView(headerView);
多数民众赞成......
答案 1 :(得分:14)
新支持库更新(23.1.1)后,
你可以这样做 -
在导航视图内的app:headerLayout="@layout/drawer_header"
中添加标题视图。
然后,您可以通过
访问它View header = navigationView.getHeaderView(0);
TextView text = (TextView) header.findViewById(R.id.textView);
或者如果您有多个标题
navigationView.getHeaderCount()
参考:https://code.google.com/p/android/issues/detail?id=190226#c31
答案 2 :(得分:6)
TextView txt2;
txt2 = (TextView) navigationView.inflateHeaderView(R.layout.nav_header_main).findViewById(R.id.textView2);
txt2.setText("wow! It works like a charm");
答案 3 :(得分:2)
创建标题布局内部的文本视图,
<TextView
android:id="@+id/profile_email_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/profile_image"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@id/expand_account_box_indicator"
android:ellipsize="end"
android:maxLines="1"
android:paddingBottom="16dp"
android:singleLine="true"
android:clickable="true"
android:onClick="onSelectText"
android:text="dhaval0122@gmail.com"
android:textColor="@color/body_text_2_inverse"
android:textSize="@dimen/text_size_medium" />
in onCreate,
((TextView) findViewById(R.id.profile_email_text)).setText("test");
在您的活动中创建方法onSelectText
public void onSelectText(View v){
if(v.getId() == R.id.profile_email_text){
Snackbar
.make(fab, "clicked on sub title", Snackbar.LENGTH_LONG)
//.setAction(R.string.snackbar_action, myOnClickListener)
.show();
drawer_layout.closeDrawers();
}
}
答案 4 :(得分:1)
您可以通过在导航视图上调用Iterator it<String> = names.iterator();
以编程方式添加自定义标头,或使用addHeaderView
在布局文件中对其进行定义。
答案 5 :(得分:1)
您可以使用//Detect Browser
var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
// At least Safari 3+: "[object HTMLElementConstructor]"
var isChrome = !!window.chrome && !isOpera; // Chrome 1+
var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6
var Host = window.location.host;
//Clear Basic Realm Authentication
if(isIE){
//IE
document.execCommand("ClearAuthenticationCache");
window.location = '/';
}
else if(isSafari)
{//Safari. but this works mostly on all browser except chrome
(function(safeLocation){
var outcome, u, m = "You should be logged out now.";
// IE has a simple solution for it - API:
try { outcome = document.execCommand("ClearAuthenticationCache") }catch(e){}
// Other browsers need a larger solution - AJAX call with special user name - 'logout'.
if (!outcome) {
// Let's create an xmlhttp object
outcome = (function(x){
if (x) {
// the reason we use "random" value for password is
// that browsers cache requests. changing
// password effectively behaves like cache-busing.
x.open("HEAD", safeLocation || location.href, true, "logout", (new Date()).getTime().toString())
x.send("");
// x.abort()
return 1 // this is **speculative** "We are done."
} else {
return
}
})(window.XMLHttpRequest ? new window.XMLHttpRequest() : ( window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : u ))
}
if (!outcome) {
m = "Your browser is too old or too weird to support log out functionality. Close all windows and restart the browser."
}
alert(m);
window.location = '/';
// return !!outcome
})(/*if present URI does not return 200 OK for GET, set some other 200 OK location here*/)
}
else{
//Firefox,Chrome
window.location = 'http://log:out@'+Host+'/';
}
访问NavigationView中的标题元素。即使您已使用headerLayout属性初始化标头,即使这样也可以。 findViewById()
。然后,您可以动态修改标头,而无需膨胀或添加新标头。
app:headerLayout="@layout/drawer_header"
<强> drawer_header.xml 强>
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
...
if(mNavItemId == R.id.drawer_item_1)
{
View headerView = mNavigationView.findViewById(R.id.drawer_header_root);
// Test modifying the size of the header root element (FrameLayout)
// when the first menu item is clicked.
LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) headerView.getLayoutParams();
p.height = p.height == 700 ? 400 : 700;
headerView.setLayoutParams(p);
return true;
}
...
我认为Dhawal说同样的话,但它并不是非常清楚。
答案 6 :(得分:0)
我的链接会帮助你
final NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
View headView = navigationView.getHeaderView(0);
((TextView) headView.findViewById(R.id.nav_title)).setText("New title");
答案 7 :(得分:0)
首先你需要获得navigationView。
{
"query": {
"count": 1,
"created": "2015-12-27T23:43:58Z",
"lang": "en-US",
"diagnostics": {
"publiclyCallable": "true",
"url": {
"execution-start-time": "28",
"execution-stop-time": "98",
"execution-time": "70",
"content": "http://wws.geotech.yahooapis.com/v1/countries;start=0;count=1000"
},
"user-time": "103",
"service-time": "70",
"build-version": "0.2.369"
},
"results": {
"place": {
"lang": "en-US" // wrong language!
}
}
}
}
然后标题。
NavigationView navigationView =(NavigationView)findViewById(R.id.nav_view);
然后是textView。
View header = navigationView.getHeaderView(0)
最后,您可以设置要显示的文本。
TextView text = (TextView) header.findViewById(R.id.textView);